Golang func keyword
last modified May 7, 2025
This tutorial explains how to use the func
keyword in Go. We'll
cover function basics with practical examples of defining and using functions.
The func keyword is used to declare functions in Go. Functions are self-contained blocks of code that perform specific tasks and can be reused.
In Go, func
can define both standalone functions and methods
attached to types. Functions are first-class citizens in Go's type system.
Basic function declaration
The simplest use of func
creates a function that takes no
parameters and returns no values. This example shows a basic function.
package main import "fmt" func greet() { fmt.Println("Hello, World!") } func main() { greet() }
The greet
function is declared with func
and called
from main
. It prints a message when executed.
Function with parameters
Functions can accept parameters. This example demonstrates a function that takes two integers and returns their sum.
package main import "fmt" func add(a int, b int) int { return a + b } func main() { sum := add(5, 7) fmt.Println("Sum:", sum) }
The add
function takes two int
parameters and returns
their sum. Parameters with the same type can be declared together.
Multiple return values
Go functions can return multiple values. This example shows a function that returns both quotient and remainder.
package main import "fmt" func divide(dividend, divisor int) (int, int) { quotient := dividend / divisor remainder := dividend % divisor return quotient, remainder } func main() { q, r := divide(10, 3) fmt.Printf("10 / 3 = %d with remainder %d\n", q, r) }
The return types are specified in parentheses. Multiple values are returned with a comma-separated list.
Named return values
Go allows naming return values for documentation and clarity. This example demonstrates named returns.
package main import "fmt" func rectangleProps(length, width float64) (area, perimeter float64) { area = length * width perimeter = 2 * (length + width) return } func main() { a, p := rectangleProps(5.0, 3.0) fmt.Printf("Area: %.2f, Perimeter: %.2f\n", a, p) }
Named returns are declared in the function signature. A bare return
statement returns the current values of these variables.
Variadic functions
Variadic functions accept a variable number of arguments. This example shows a function that sums any number of integers.
package main import "fmt" func sum(numbers ...int) int { total := 0 for _, num := range numbers { total += num } return total } func main() { fmt.Println("Sum of 1, 2, 3:", sum(1, 2, 3)) fmt.Println("Sum of 4, 5, 6, 7:", sum(4, 5, 6, 7)) }
The ...
prefix makes the parameter variadic. Inside the function,
it's treated as a slice of the specified type.
Function as a type
Functions are first-class citizens in Go. This example shows how to use functions as values and types.
package main import "fmt" type mathFunc func(int, int) int func add(a, b int) int { return a + b } func multiply(a, b int) int { return a * b } func calculate(a, b int, operation mathFunc) int { return operation(a, b) } func main() { sum := calculate(5, 3, add) product := calculate(5, 3, multiply) fmt.Println("Sum:", sum) fmt.Println("Product:", product) }
We define a mathFunc
type for functions with specific signature.
Functions can be passed as arguments and stored in variables.
Method declaration
The func
keyword is also used to define methods on types. This
example shows a method attached to a struct.
package main import "fmt" type Rectangle struct { length, width float64 } func (r Rectangle) area() float64 { return r.length * r.width } func main() { rect := Rectangle{length: 5.0, width: 3.0} fmt.Println("Area:", rect.area()) }
The method receiver (r Rectangle)
appears before the function name.
Methods are called on instances of the type using dot notation.
Source
This tutorial covered the func
keyword in Go with practical
examples of function and method declarations in various scenarios.
Author
List all Golang tutorials.