Learning Go 2 - Package and Receiver Function

2 Package types: Executable and Reusable.

Executable package main must have a function called "main". Reusable packages are like helper packages or libraries.

We can call the function in other go file of the same package main.

Let's say we have "main.go" and the receiver function in "type.go" file. We can call the receiver function in the main function, and no need to write import. Just type the command go run main.go type.go and we can execute both files.

Receiver Function

Any variable that is type deck can use this print method.

type deck []string

func (d deck) print() {
    for i, card := range d {
        fmt.Println(i, card)
    }
}

cards := deck{"Ace", "King"}
cards.print() 
// 0,Ace 
// 1,King

By convention, the receiver is the first letter of type.

Reference: Go: The Complete Developer's Guide of Stephen Grider.