Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Created September 30, 2014 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/cb08d2d0bdcb884f22d1 to your computer and use it in GitHub Desktop.
Save jochasinga/cb08d2d0bdcb884f22d1 to your computer and use it in GitHub Desktop.
Variables in Go
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