Skip to content

Instantly share code, notes, and snippets.

@lilbond
Created February 2, 2017 00:10
Show Gist options
  • Select an option

  • Save lilbond/268fb4ed040b096f21c7f65c080654eb to your computer and use it in GitHub Desktop.

Select an option

Save lilbond/268fb4ed040b096f21c7f65c080654eb to your computer and use it in GitHub Desktop.
Basics of variables in Go. An explanation is provided here http://www.musingscafe.com/golang-part-1-variables/
package main
import (
"fmt"
)
func main() {
shorthand()
}
func printDefaultValue() {
var aBoolean bool
var anInt int
var aString string
var aFloat float64
fmt.Println(aBoolean)
fmt.Println(anInt)
if aString == "" {
fmt.Println("aString was set correctly")
}
fmt.Println(aFloat)
}
func shorthand() {
inferedCount := 9000
fmt.Println(inferedCount) //would print 9000
//try setting inferedCount again
//inferedCount := 1000
//compilation error := is not assignment operator
//no new variables on left side of :=
//inferedCount = "Do" //compilation error as string can not be assigned to int
//but this will work
name, inferedCount := "Any", 10000
fmt.Println(inferedCount) //would print 9000
fmt.Println(name)
//multiple variables
var i, j int
fmt.Println(i)
fmt.Println(j)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment