Created
February 2, 2017 00:10
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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