Created
September 30, 2014 00:51
-
-
Save jochasinga/cb08d2d0bdcb884f22d1 to your computer and use it in GitHub Desktop.
Variables in Go
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" | |
var global1 int // Declaring a variable with `var` keyword | |
var global2 int = 10 // Assigning a value to a variable right away | |
var global3 = "this is ok!" // Go infer type based on your value | |
// bad_var := "this doesn't work" // `:=` can be used only inside a function | |
func main() { | |
local1 := 9 // Inside a function, Go use type inferrence | |
// so that you don't have to state type explicitly | |
var local2 = 5 // This is ok | |
var local3 = 7 // This is ok too | |
fmt.Println(global1, | |
global2, | |
global3, | |
local1, | |
local2, | |
local3, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment