basics Flashcards
(11 cards)
Front
Back
How do you declare a variable in Go?
var x int = 42
What’s the short variable declaration syntax in Go?
x := 42
How do you define a function in Go?
func add(a int, b int) int { return a + b }
How do you define a struct in Go?
type Person struct { Name string; Age int }
How do you write a for loop in Go?
for i := 0; i < 10; i++ { fmt.Println(i) }
How do you create a slice from an array in Go?
slice := arr[1:4]
How do you return multiple values from a function in Go?
a, b := myFunc()
How do you define an interface in Go?
type Reader interface { Read(p []byte) (n int, err error) }
How do you write a switch statement in Go?
switch x := time.Now().Weekday(); x { case time.Saturday: … }
How do you defer a function call in Go?
defer fmt.Println(“done”)