1/17
Common declarations and formatting of code in Go
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
variable declaration without a corresponding initialization
var x intvariable declaration shorthand syntax within golang functions
:=variable declaration with declared type
var a string = "initial"for-loop w/ single condition
for i <= 3 {
fmt.Println(i)
i = i + 1
}classic for-loop
for j:= 0; j < 3; j++ {
fmt.Println(j)
}for-loop w/ range
for i := range 3 {
fmt.Println("range", i)
}for-loop w/out condition
for {
fmt.Println("loop")
break
}basic if/else
if a = true {
fmt.Println("True")
else {
fmt.Println("False")
}basic switch statement
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")array declaration of size 5
var a [5]intarray declaration of size 5 with initialization of 5 ints
b = [5]int{1, 2, 3, 4, 5}Array Declaration w/ initializations and complier determined size
b = [...]int{1, 2, 3, 4, 5}2D Array declaration
twoD := [2][3]int{
{1, 2, 3},
{1, 2, 3},
}basic slice declaration of type string
var s []stringSlice Declaration w/ non-zero length and capacity
s = make([]string, length, capacity)Copy a Slice
var s = []string{"a", "b", "c"}
c := make([]string, len(s))
copy(c, s)2D Slice
twoD := [][]int{
{1, 2, 3},
{4, 5, 6},
}