diff --git a/examples.txt b/examples.txt index dc189e3..6af5458 100644 --- a/examples.txt +++ b/examples.txt @@ -1,7 +1,6 @@ Hello World Values -# Variables -# Short Declarations +Variables # Constants # For If/Else diff --git a/examples/values/values.go b/examples/values/values.go index bbaac3d..4618c67 100644 --- a/examples/values/values.go +++ b/examples/values/values.go @@ -9,7 +9,7 @@ import "fmt" func main() { // Strings, which can be added together with `+`. - fmt.Println("Hello " + "world") + fmt.Println("go" + "lang") // Integers and floats. fmt.Println("1+1 =", 1+1) diff --git a/examples/values/values.sh b/examples/values/values.sh index 050a4a0..26181ed 100644 --- a/examples/values/values.sh +++ b/examples/values/values.sh @@ -1,5 +1,5 @@ $ go run values.go -Hello world +golang 1+1 = 2 7.0/3.0 = 2.3333333333333335 false diff --git a/examples/variables/variables.go b/examples/variables/variables.go index 934f111..0398429 100644 --- a/examples/variables/variables.go +++ b/examples/variables/variables.go @@ -1,14 +1,35 @@ +// In Go, variables are explicitly declared and used by +// the compiler to e.g. check type-correctness of function +// calls. + package main import "fmt" func main() { - // `var` declares 1 or more variables. The type comes - // at the end. - var x string = "Hello world" - fmt.Println(x) - // An example of declaring multiple `int` variables. - var a, b int = 1, 2 - fmt.Println(a, b) + // `var` declares 1 or more variables. The type comes + // _after_ the name of the variable. + var a string = "Initial" + fmt.Println(a) + + // You can declare multiple variables at once. + var b, c int = 1, 2 + fmt.Println(b, c) + + // Go will infer the type of initialized variables. + var d = true + fmt.Println(d) + + // Variables declared without a corresponding + // initialization are _zero-valued_. For example the + // zero value for an `int` is `0`. + var e int + fmt.Println(e) + + // The `:=` syntax is shorthand for declaring and + // initializing a variable, e.g. for + // `var f string = "Short"` in this case. + f := "Short" + fmt.Println(f) } diff --git a/examples/variables/variables.sh b/examples/variables/variables.sh index 9da3671..216a4e1 100644 --- a/examples/variables/variables.sh +++ b/examples/variables/variables.sh @@ -1,3 +1,6 @@ $ go run variables.go -Hello world +Initial 1 2 +true +0 +Short