Go Tutorial Part 4 Functions
Contents
1 Simple Function
package main
import "fmt"
func main() {
card := newCard()
fmt.Println(card)
}
func newCard() string {
return "Five of Diamonds"
}
newCard()
: define function of namestring
: 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)