MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Functions

Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.

Functions will not be called automatically once an application starts or load.

In Go, functions are written in a specific way:

  • You will need to use the keyword func To define a function in Go.
  • It has to have a name followed by open and closing parentheses.
  • The set of code to be executed by the function should be surrounded by open and closed braces.
  • Functions names must start with a letter only.
  • Functions names can only contain alpha-numeric and underscores(A-z, 0-9 and _).
  • Functions Names are case sensitive meaning you have to use the same name in uppercase or lowercase.

Go Function creation Syntax

func Function_Name() {
  // set of code to be executed
}

Example of Go Function

package main
import ("fmt")

func say_hello_world() {
  fmt.Println("Hello World")
}

func main() {
  say_hello_world() // call the say_hello_world function
}

Output

Hello World

Conclusion

In this page (written and validated by ) you learned about Go Functions . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Function Parameters and arguments.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.