Functions in Golang
Functions in Go language, are simply like other other programming languages, are a code block which can be called every time we need it in our code rather than re-writing it again and again. They takes one or multiple inputs and return output or errors.
Syntax to define functions in Go language:
Let's first discuss about the syntax to use functions in Golang.
func <name> (<inputs>) (<datatypes of return values>) {
// your code here
}
- Function in Golang are defined with
func
keyword.
name
- The name that we use to call the function.
inputs
- Single or multiple inputs that should be provided to the function while calling. (We can also specify the data type for the input values.)
datatypes of return values
- Output of the function. (we can also assign a variable to the returned value from the function.)
code
- The code of function should be placed inside the brackets with proper identation.
Basic function in Go language
To start with functions in Golang, we can take example of a simple greetings function which takes a user name as string and return the string greetings message as output:
package main
import "fmt"
func greetings(name string) string {
message := "Hello " + name + ", welcome to studytonight!"
return message
}
func main() {
username := "user"
fmt.Println(greetings(username))
}
Hello user, welcome to studytonight!
main
function in Golang
The main
function is a special function in Go that is automatically called when a Go program is executed. The main
function typically contains the code that runs when the program starts. Here is an example of a main
function:
func main() {
fmt.Println("This is the main function")
}
init
function in Golang
The init
function is another special function in Go that is automatically called before the main
function. The init
function is typically used to initialize variables or perform setup tasks that need to be done before the main program runs. Here is an example of an init
function:
func init() {
fmt.Println("This is the init function")
}
Functions vs Methods
In Go, functions are different from methods. A method is a function that is associated with a specific type, whereas a function is not associated with any type and can be called by itself. Here is an example of a method in Go:
type Person struct {
name string
}
func (p *Person) sayHello() {
fmt.Printf("Hello, my name is %s\n", p.name)
}
How to call a function from another package?
To call a function from another package in Go, you need to import the package and then use the fully qualified function name, which includes the package name and the function name. For example, if you wanted to call the hello
function defined above from another package, you could do it like this:
import "mypackage"
mypackage.hello() // Output: Hello, world!
Functions in parameters - Golang
Yes, functions can be passed as parameters in Go. This is called function currying, and it allows you to create functions with variable number of arguments. Here is an example of passing a function as a parameter in Go:
func add(x, y int) int {
return x + y
}
func apply(f func(int, int) int, a, b int) int {
return f(a, b)
}
result := apply(add, 1, 2) // Output: 3
In this example, the add
function is passed as a parameter to the apply
function, which then calls the add
function and returns the result.
Conclusion
In conclusion, functions in Go language are blocks of code that can be called whenever needed in the code. They take one or multiple inputs and return output or errors. Functions are defined using the func
keyword and have a specific syntax, including the function name, input values, and return values. Go also has special functions called main
and init
, which are automatically called when a program is executed. In addition, Go has a distinction between functions and methods, where a method is associated with a specific type. Functions can also be passed as parameters in Go, which is called function currying.