Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package main
  
import (
  "fmt"
)

func main(){
  var x int
  x = 5
  fmt.Println(x)
}

Loops

...

x := 7
  
  if x > 6 {
    fmt.Println("More than 6")
  } else if x < 2 {
  
  } else {

  }
}

Arrays

Code Block
package main
  
import (
  "fmt"
)

func main(){
  //fix size
  var a [5]int
  a[2] = 7
  fmt.Println(a)

  a:= [5]int{5,4,3,2,1}
  fmt.Println(a)

  //slices
  a:= [] int{5,4,3,2,1}
  a = append(a,13)	
  fmt.Println(a)

}

Maps

Code Block
package main
  
import (
  "fmt"
)

func main(){

  vertices := make(map[string]int)

  vertices["triangle"] = 2
  vertices["square"] = 3
  vertices["tree"] = 12

  delete(vertices, "square")

  fmt.Println(vertices["traingle"])
  fmt.Println(vertices)


}

Loops

The only type of look in go is the for loop.

Code Block
package main
  
import (
  "fmt"
)

func main(){

  //for loop
  for i :=0; i<5; i++{
    fmt.Println(i)
  }

  //while loop
  j:=0  
  for j<5 {
    fmt.Println(j)
    j++
  }

  //loop over array or slice
  arr := []string{"a"m"b","c"}
  for index, value := range arr {
	fmt.Println("index:", index, "value:", value)
  }


  //loop over a map	
  m := make(map[string]int)
  m["triangle"] = 2
  m["square"] = 3
  for key, value := range m {
	fmt.Println("key:", key, "value:", value)
  }

}

Functions 

Example showing error return

...