main

The first function

The first function in a GO program to be called is the function main located in package main.

Declare a function

Symbolic:

func name(arguments) returntype {
    // do something
}

Example:

func say(name string) boolean {
    fmt.Println(name)
    return true
}

If you have multiple return types, the declaration is:

func name(arguments) (returntypes) {
    // do something
}

Calling a function

A function within the same package can be called directly.

(not runnable code)

func main(){
    say("Bob")
    
}

func say(name string){
    fmt.Println(name)
}

A function from an imported package must be:

  • Starting with an uppercase character
  • preceded by the package name

import "fmt"

func main(){
    fmt.Println("Bob")
    
}

Next: Function Arguments

See also