publish variables, inline short declarations for now
This commit is contained in:
parent
cdc3184c30
commit
9f795ce5f3
@ -1,7 +1,6 @@
|
||||
Hello World
|
||||
Values
|
||||
# Variables
|
||||
# Short Declarations
|
||||
Variables
|
||||
# Constants
|
||||
# For
|
||||
If/Else
|
||||
|
@ -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)
|
||||
|
@ -1,5 +1,5 @@
|
||||
$ go run values.go
|
||||
Hello world
|
||||
golang
|
||||
1+1 = 2
|
||||
7.0/3.0 = 2.3333333333333335
|
||||
false
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
$ go run variables.go
|
||||
Hello world
|
||||
Initial
|
||||
1 2
|
||||
true
|
||||
0
|
||||
Short
|
||||
|
Loading…
x
Reference in New Issue
Block a user