Go Programming Language Functions

Go Programming Language Functions

Explains how to create function in Go programming.

Syntax:
func function_name return_data_type {
	// task
	return 
}

here return_data_type and return code is optional.

Function with no parameters

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
	fmt.Println("main function - before show called")                      	 
	show()                                                                 	 
	fmt.Println("main function - after show called")                       	 
}                                                                          	 
                                                                           	 
func show() {                                                              	 
	fmt.Println("Go user defined function 'show' is called")               	 
}
Output:
$ go build function.go
$ ./function
main function - before show called
Go user defined function 'show' is called
main function - after show called

Function with Parameters

Go function can have one or more parameters.

Below code is used to create function with parameters.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
	fmt.Println("main function - before show called")                      	 
	msg := "testing"                                                       	 
	show(msg)                                                              	 
	fmt.Println("main function - after show called")                       	 
}                                                                          	 
                                                                           	 
func show(msg string) {                                                    	 
	fmt.Printf("Go user defined function 'show' is called: msg(%s)\n", msg)	 
} 
Output:
$ go build function.go
$ ./function
main function - before show called
Go user defined function 'show' is called: msg(testing)
main function - after show called

Function with returning value

Function can also return one or more return values.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
	var a,b,c int                                                          	 
                                                                           	 
	a = 19                                                                 	 
	b = 5                                                                  	 
                                                                           	 
	c = add(a,b)                                                           	 
	fmt.Printf("%d + %d: %d\n", a, b, c)                                   	 
}                                                                          	 
                                                                           	 
func add(a,b int) int {                                                    	 
	return a+b                                                             	 
} 
Output:
$ go build function.go
$ ./function
19 + 5: 24