publish variables, inline short declarations for now

This commit is contained in:
Mark McGranaghan 2012-10-17 13:07:31 -07:00
parent cdc3184c30
commit 9f795ce5f3
5 changed files with 35 additions and 12 deletions

View File

@ -1,7 +1,6 @@
Hello World Hello World
Values Values
# Variables Variables
# Short Declarations
# Constants # Constants
# For # For
If/Else If/Else

View File

@ -9,7 +9,7 @@ import "fmt"
func main() { func main() {
// Strings, which can be added together with `+`. // Strings, which can be added together with `+`.
fmt.Println("Hello " + "world") fmt.Println("go" + "lang")
// Integers and floats. // Integers and floats.
fmt.Println("1+1 =", 1+1) fmt.Println("1+1 =", 1+1)

View File

@ -1,5 +1,5 @@
$ go run values.go $ go run values.go
Hello world golang
1+1 = 2 1+1 = 2
7.0/3.0 = 2.3333333333333335 7.0/3.0 = 2.3333333333333335
false false

View File

@ -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 package main
import "fmt" import "fmt"
func main() { 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` declares 1 or more variables. The type comes
var a, b int = 1, 2 // _after_ the name of the variable.
fmt.Println(a, b) 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)
} }

View File

@ -1,3 +1,6 @@
$ go run variables.go $ go run variables.go
Hello world Initial
1 2 1 2
true
0
Short