bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Go/Go Tutorial
Go•Go Tutorial

Go Functions

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Go Functions?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___
3Order

Put the learning moves in the order that makes the concept easiest to apply.

A function will not execute automatically when a page loads.
A function is a block of statements that can be used repeatedly in a program.
Naming Rules for 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
}

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()
}

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!

Previous

Go For Loops

Next

Go Function Parameters and Arguments