Category : Go lang | Sub Category : Hands on Golang | By Prasad Bonam Last updated: 2024-07-01 05:38:16 Viewed : 165
GO Functions
These examples cover the basics of functions in Go, including defining functions, using multiple parameters and return values, working with variadic functions, using anonymous functions and closures, and defining methods on types. Functions are a fundamental part of Go programming, providing a clear and efficient way to organize and reuse code.
A basic function in Go includes a name, parameters, and a return type.
package main
import (
"fmt"
)
// greet is a function that takes a name as a
string and returns a greeting message as a string.
func greet(name string) string {
return "Hello,
" + name
}
func main() {
message := greet("Alice")
fmt.Println(message) // Output: Hello, Alice
}
Functions can take multiple parameters of different types.
package main
import (
"fmt"
)
// add is a function that takes two integers
and returns their sum.
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 4)
fmt.Println(result) // Output: 7
}3. Functions with Multiple Return Values
Go supports multiple return values, which is useful for returning error
values along with results.
package main
import (
"fmt"
"strconv"
)
// divide is a function that divides two
integers and returns the result and an error if any.
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("cannot
divide by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result) // Output:
Result: 5
}
_,
err = divide(10, 0)
if err != nil {
fmt.Println("Error:", err) // Output: Error:
cannot divide by zero
}
Functions can specify named return values, which can simplify the function
body by using a return
statement without arguments.
package main
import (
"fmt"
)
// swap is a function that swaps two strings.
func swap(a, b string) (x, y string) {
x =
b
y =
a
return
}
func main() {
a,
b := swap("hello", "world")
fmt.Println(a, b) // Output: world hello
}
5. Variadic Functions
Variadic functions can accept a variable number of arguments.
package main
import (
"fmt"
)
// sum is a variadic function that sums a
variable number of integers.
func sum(numbers ...int) int {
total := 0
for _, number := range numbers {
total += number
}
return total
}
func main() {
fmt.Println(sum(1, 2, 3)) // Output: 6
fmt.Println(sum(1, 2, 3, 4, 5)) // Output: 15
}
Anonymous functions are functions without a name and can be defined inline.
package main
import (
"fmt"
)
func main() {
// Anonymous function assigned to a variable
greet := func(name string) string {
return "Hello, " + name
}
message := greet("Bob")
fmt.Println(message) // Output: Hello, Bob
// Anonymous function used directly
func(a, b int) {
fmt.Println(a + b) // Output: 5
}(2, 3)
}
Closures are anonymous functions that capture and use variables from their surrounding scope.
package main
import (
"fmt"
)
// adder returns a closure that adds an
integer to its internal sum.
func adder() func(int) int {
sum
:= 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
add
:= adder()
fmt.Println(add(1)) // Output: 1
fmt.Println(add(2)) // Output: 3
fmt.Println(add(3)) // Output: 6
Go supports methods defined on types, including struct types.
package main
import (
"fmt"
)
type Rectangle struct {
Width, Height int
}
// Area is a method on the Rectangle type
that calculates the area of the rectangle.
func (r Rectangle) Area() int {
return r.Width * r.Height
}
func main() {
rect := Rectangle{Width: 10, Height: 5}
fmt.Println("Area:", rect.Area()) // Output: Area: 50
}