HomeToolsAbout a20k

Function

What is it

func HelloWorld() string { return "Hello World" }
  • Function name fo HelloWorld
  • returns string type
func add(x int, y int) int { return x + y }
  • Defines a function of name add
  • Defines Params
    • accept parameter of x which is of type int
    • accept parameter of y which is also of type int
  • Defines Return type
    • function returns type of int

Return deconstruction

When a function returns a err as second return value, you can use it to catch error

result, err := package.method() if err != nil { log.Printf("error calling package.method: %v", err) }

Function Parameter and Type Syntax

(i item) is stating that function process() operates on an object of type item

  • item is a struct defined
  • item is used via item.id and item.price

process() is the name of the function with standard parameter

  • In this case, no parameter is passed to the function

(string, int) states that the function has multiple return values of string and integer types

func (i item) process() (string, int) { return i.id, i.price } // above the file type item struct { id string price int }
© VincentVanKoh