Functions in Go Language





Hey guys in this post, we will discuss the functions and how to call functions in Golang with easy to understand examples.

1. Create a directory


mkdir go-workspace

2. Move inside the directory


cd go-workspace

3. Create an empty file


code Functions.go

Keyword code represents Visual studio code, it will open the file inside the Visual studio code.

Note: I will be using Visual studio code for writing code but you can use any other text editors or IDEs for writing code.

4. Write the Code


package main

import "fmt"

func main() {
	subFuction()
}

func subFuction() {
	fmt.Println("Calling another function inside main() function!")
}
  • A function is a group of statements that exist within a program for the purpose of performing a specific task
  • In Golang we use func keyword to create functions
  • In fact, the main() method itself is a function
  • We can call functions inside any other functions, any number of time.

5. Run the Code


To run the program, open the command prompt and execute the following command

PS C:\workspace\go lang workspace> go run .\Functions.go
Calling another function inside main() function!




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply