Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Go Functions


A function is a block of statements that can be used repeatedly in a program.

A function will not execute automatically when a page loads.

A function will be executed by a call to the function.


Create a Function

To create (often referred to as declare) a function, do the following:

  • Use the func keyword.
  • Specify a name for the function, followed by parentheses ().
  • Finally, add code that defines what the function should do, inside curly braces {}.

Syntax

func FunctionName() {
  // code to be executed
}

Call a Function

Functions are not executed immediately. They are "saved for later use", and will be executed when they are called.

In the example below, we create a function named "myMessage()". The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs "I just got executed!". To call the function, just write its name followed by two parentheses ():

Example

package main
import ("fmt")

func myMessage() {
  fmt.Println("I just got executed!")
}

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

Result:

I just got executed!
Try it Yourself »

A function can be called multiple times.

Example

package main
import ("fmt")

func myMessage() {
  fmt.Println("I just got executed!")
}

func main() {
  myMessage()
  myMessage()
  myMessage()
}

Result:

I just got executed!
I just got executed!
I just got executed!
Try it Yourself »


Naming Rules for Go Functions

  • A function name must start with a letter
  • A function name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Function names are case-sensitive
  • A function name cannot contain spaces
  • If the function name consists of multiple words, techniques introduced for multi-word variable naming can be used

Tip: Give the function a name that reflects what the function does!


Go Exercises

Test Yourself With Exercises

Exercise:

Create a function named myFunction and call it inside main().

package main   
import ("fmt")
func { fmt.Println("I just got executed!") }
func main() { }

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.