DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Functions and Methods in Go

Functions and Methods in Go

Go is a powerful programming language that allows developers to create efficient and scalable software. One of the key features of Go is its support for functions and methods, which enable developers to organize their code into reusable blocks.

Functions

In Go, a function is a self-contained block of code that performs a specific task. It can be called from anywhere in the program, making it easy to reuse code and improve modularity. Here's how you can define and use functions in Go:

func add(a int, b int) int {
    return a + b
}

func main() {
    result := add(3, 5)
    fmt.Println(result)  // Output: 8
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we defined a function named add that takes two integer parameters a and b. It returns the sum of these two numbers. In the main function, we called the add function with arguments 3 and 5, assigning the returned value to a variable named result. Finally, we printed out the result using the built-in fmt.Println() function.

Go also supports multiple return values in functions:

func divide(dividend float64, divisor float64) (float64,error) {
    if divisor == 0 {
        return 0, errors.New("cannot divide by zero")
    }
    return dividend / divisor,nil
}

func main() {
    result,err := divide(10.0 ,2.0)
    if err != nil{
        fmt.Println(err)
    }else{
        fmt.Println(result) //Output: 5 
    }
}
Enter fullscreen mode Exit fullscreen mode

Here's an example where our function returns both quotient (float64) as well as an error (error) type if division by zero occurs.

Methods

Methods are similar to functions but are associated with types in Go. They define actions that can be performed on objects of a particular type. This feature enables us to achieve code organization and encapsulation efficiently.

To declare a method in Go, we use the following syntax:

func (s StructName) methodName() {
    // method implementation
}
Enter fullscreen mode Exit fullscreen mode

Let's look at an example:

type Circle struct {
    radius float64
}

func (c Circle) area() float64 {
    return math.Pi * c.radius * c.radius
}

func main() {
    circle := Circle{radius: 5}
    fmt.Println(circle.area())  // Output: 78.53981633974483 
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we defined a Circle struct with a single field radius. We then declared an area() method for the Circle struct that calculates and returns the area of the circle based on its radius using the formula πr².

To call this method, we create an instance of the Circle struct named circle, and then invoke its area() method using dot notation (circle.area()). The result is printed to the console using fmt.Println().

Methods can also have additional parameters just like functions if needed.

Conclusion

Go provides flexible support for both functions and methods, allowing developers to write clean, modular, and reusable code. Functions offer standalone functionality while methods provide functionality specific to types. By understanding how to use these features effectively, you can harness the full power of Go in your programming endeavors.

Top comments (0)