Contents

Go Tutorial Part 4 Functions

1 Simple Function

package main

import "fmt"

func main() {
	card := newCard()
	fmt.Println(card)
}

func newCard() string {
	return "Five of Diamonds"
}
  1. newCard(): define function of name
  2. string: return datatype

2 Import

Files in the same package do not have to be imported

main.go

package main
  
func main() {
    printState()
}

state.go

package main
  
import "fmt"
  
func printState() {
    fmt.Println("California")
}

3 Returning multiple values

func deal(d deck, handSize int) (deck, deck) {
	return d[:handSize], d[handSize:]
}

hand, remainingCards := deal(cards, 5)