Functions in Go
A function is a block of statements which performs a specific task. A function is a well-organized and reusable code. It improves the code readability, maintainability and testability. The general function is:
func function_name( [parameter list] ) [return_types]
{
body of the function
}
Declaring and calling functions
The function is declared using func
the keyword.
func sayCheeze() {
fmt.Println("Cheeeeeeeeeeeeze")
}
Calling the function is pretty easy.
sayCheeze()
package main
import "fmt"
func sayCheeze() {
fmt.Println("Cheeeeeeeeeeeeze")
}
func main() {
sayCheeze()
}
Run this code in Go Playground
You may pass input parameters
func addition(i int, j int) {
fmt.Println(i + j)
}
We need to pass the declared number of parameters while calling the function.
addition(1, 2)
When two or more consecutive parameters have the same type we can omit the type from all but the last.
package main
import "fmt"
func addition(i, j, k int) {
fmt.Println(i + j + k)
}
func main() {
addition(10, 20, 30)
}
Run this code in Go Playground
You may define the return type of the function
func addition(i, j, k int) int {
return i + j + k
}
sum := addition(1, 2, 3)
A function can return any number of results.
package main
import "fmt"
func addition(i, j int) (int, int, int) {
return i, j, i + j
}
func main() {
a, b, sum := addition(1, 2)
fmt.Println(a, b, sum)
}
Run this code in Go Playground
Named return values
Go's return values may be named. The named return values are treated as locally defined variables in the function.
package main
import "fmt"
func division(i int) (quotient, remainder int) {
quotient = i / 10
remainder = i % 10
return
}
func main() {
quotient, remainder := division(125)
fmt.Printf("Quotient: %d and Remainder: %d", quotient, remainder)
}
Run this code in Go Playground
Variadic functions
Variadic functions can be called with any number of trailing arguments. For example, fmt.Println
is a common variadic function.
package main
import "fmt"
func total(nums ...int) (total int) {
for _, i := range nums {
total += i
}
return total
}
func main() {
fmt.Println(total(1, 2, 3, 4, 5))
fmt.Println(total(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
}
Run this code in Go Playground
Thank you for reading this blog, and please give your feedback in the comment section below.
Subscribe to my newsletter
Read articles from Pratik Jagrut directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pratik Jagrut
Pratik Jagrut
๐ Hey there! I'm a community-driven software engineer, and I absolutely love diving into the world of cloud-native development and exploring the endless possibilities of open-source technologies. ๐ป My skill set revolves around Kubernetes, GoLang, Python, Docker, and other fascinating container technologies. ๐ And hey, just to add to the mix, I'm also CKA certified! ๐ But you know what? My journey doesn't stop at coding. I have a genuine passion for technical evangelism. ๐ค I've had the privilege to speak at world-renowned events, sharing my knowledge and insights with others. It's such an exhilarating experience! And when I'm not on stage, you can find me pouring my thoughts into engaging technical blogs. ๐ If you've made it this far and you're intrigued by what you've read, let's not leave it at that! Reach out, and let's connect. Who knows what exciting opportunities may be awaiting us? ๐