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 you have multiple return values on a function, it must be wrapped in a parenthesis ()

func twoReturns() (r1 string, r2 string) { // ... }

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

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

Parameter and Type Breakdown

First parameter is stating that function operates on an object of defined name and type

  • item is a struct defined
  • item is used via item.id and item.price
  • This is where the dependency injection using fx would be made available
func (m_mod *MathModule) FunctionName() () { arithmatics := m_mod.arithmatics }

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