1.Hello World
if we run the program, put the code in hello.go and use go run. if we want to build the program intos binaries. we can do this using go build. we can execute the built binary directly.
[maxwell@oracle-db-19c go_by_examples]$ vim hello.go
[maxwell@oracle-db-19c go_by_examples]$ go run hello.go
hello world
[maxwell@oracle-db-19c go_by_examples]$ go build hello.go
[maxwell@oracle-db-19c go_by_examples]$ ls
hello hello.go README.md
[maxwell@oracle-db-19c go_by_examples]$ ./hello
hello world
[maxwell@oracle-db-19c go_by_examples]$ cat hello.go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
[maxwell@oracle-db-19c go_by_examples]$
2.Values
Go has various types including strings,integers,floats,booleans,etc.
Strings which can be added together with + Integers and floats.
[maxwell@oracle-db-19c go_by_examples]$ vim Values.go
[maxwell@oracle-db-19c go_by_examples]$ cat Values.go
package main
import "fmt"
func main() {
fmt.Println("go" + "lang")
fmt.Println("1+1 =",1+1)
fmt.Println("7.0/3.0 =",7.0/3.0)
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
[maxwell@oracle-db-19c go_by_examples]$ go run Values.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false
[maxwell@oracle-db-19c go_by_examples]$
3.Variables
In Go,variables are explicitly declared and used by the compiler.
var declares 1 or more variables
Go will infer the type of initialized variables.
Variables declared without a corresponding initialization are zero-valued.
The := syntax is shorthand for declaring and initializing a variable.e.g.
for var f string = "apple"
[maxwell@oracle-db-19c go_by_examples]$ vim Variables.go
[maxwell@oracle-db-19c go_by_examples]$ cat Variables.go
package main
import "fmt"
func main() {
var a = "initial"
fmt.Println(a)
var b,c int = 1, 2
fmt.Println(b, c)
var d = true
fmt.Println(d)
var e int
fmt.Println(e)
f := "apple"
fmt.Println(f)
}
[maxwell@oracle-db-19c go_by_examples]$ go run Variables.go
initial
1 2
true
0
apple
[maxwell@oracle-db-19c go_by_examples]$
4. Constants
Go supports constants of character,string,boolean,and numeric values.
const declares a constant value.
A const statement can appear anywhere a var statement can.
Constant expressions perform artithmetic with arbitrary precision.
A numeric constant has no type until it's given one.
A number can be given a type by using it in a context that requires one,such as a variable assignment or function call.
[maxwell@oracle-db-19c go_by_examples]$ vim Constants.go
[maxwell@oracle-db-19c go_by_examples]$
[maxwell@oracle-db-19c go_by_examples]$ cat Constants.go
package main
import (
"fmt"
"math"
)
const s string = "constant"
func main(){
fmt.Println(s)
const n = 500000000
const d = 3e20 / n
fmt.Println(d)
fmt.Println(int64(d))
fmt.Println(math.Sin(n))
}
[maxwell@oracle-db-19c go_by_examples]$ go run Constants.go
constant
6e+11
600000000000
-0.28470407323754404
[maxwell@oracle-db-19c go_by_examples]$
5.For
for is Go's only looping construct .
The most basic type, with a single condition.
A classic initial/condition/after for loop.
for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.
[maxwell@oracle-db-19c go_by_examples]$ vim For.go
[maxwell@oracle-db-19c go_by_examples]$ cat For.go
package main
import "fmt"
func main() {
i:= 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
for j := 7;j <= 9; j++ {
fmt.Println(j)
}
for {
fmt.Println("loop")
break
}
for n := 0; n<= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}
[maxwell@oracle-db-19c go_by_examples]$ go run For.go
1
2
3
7
8
9
loop
1
3
5
[maxwell@oracle-db-19c go_by_examples]$
6.If/Else
Branching with if and else in Go is straight-forward.
you can have an if statement without an else.
A statement can precede conditionals; any variables declared in this statement are available in the current and all subsequent branches.
Note that you don't need patrentheses around conditions in Go,but that the braces are required.
[maxwell@oracle-db-19c go_by_examples]$ vim if-else.go
[maxwell@oracle-db-19c go_by_examples]$ cat if-else.go
package main
import "fmt"
func main() {
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
[maxwell@oracle-db-19c go_by_examples]$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit
[maxwell@oracle-db-19c go_by_examples]$
7. Switch
Swich statements express conditionals across many branches.
You can use commas to separate multiple expressions in the same case statement.We use the optional default case in this example as well.
switch without an expression is an alternate way to express if/else logic.Here we also show how the case expressions can be non-constants.
A type switch compares types instead of values.You can use this to discover the type of an interface value.
[maxwell@oracle-db-19c go_by_examples]$ vim Switch.go
[maxwell@oracle-db-19c go_by_examples]$ cat Switch.go
package main
import (
"fmt"
"time"
)
func main() {
i := 2
fmt.Print("Write", i, "as ")
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("three")
}
switch time.Now().Weekday(){
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's a weekday")
}
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
whatAmI := func(i interface{}) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type %T\n", t)
}
}
whatAmI(true)
whatAmI(1)
whatAmI("hey")
}
[maxwell@oracle-db-19c go_by_examples]$
[maxwell@oracle-db-19c go_by_examples]$ go run Switch.go
Write2as Two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type string
[maxwell@oracle-db-19c go_by_examples]$
8.Arrays
In Go, an array is a numbered sequence of elements of specific length,In typical Go code,slices are much more common; arrays are useful in some special scenarios.
[maxwell@oracle-db-19c go_by_examples]$ vim Arrays.go
[maxwell@oracle-db-19c go_by_examples]$ cat Arrays.go
package main
import "fmt"
func main(){
var a [5]int
fmt.Println("emp:", a)
a[4] = 100
fmt.Println("set:", a)
fmt.Println("get:", a[4])
fmt.Println("len:", len(a))
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)
var twoD [2][3]int
for i := 0; i < 2;i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2nd: ", twoD)
}
[maxwell@oracle-db-19c go_by_examples]$ go run Arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2nd: [[0 1 2] [1 2 3]]
[maxwell@oracle-db-19c go_by_examples]$
9. Slices
Slices are an important data type in Go,giving a more powerful interface to sequences than arrays.
[maxwell@oracle-db-19c go_by_examples]$ vim slices.go
[maxwell@oracle-db-19c go_by_examples]$ cat slices.go
package main
import "fmt"
func main() {
s := make([]string, 3)
fmt.Println("emp:", s)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])
fmt.Println("len:", len(s))
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd", s)
c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy:", c)
l := s[2:5]
fmt.Println("sl1:", l)
l = s[:5]
fmt.Println("sl2:", l)
l = s[2:]
fmt.Println("sl3:", l)
t := []string{"g","h","i"}
fmt.Println("dcl:", t)
twoD := make([][]int, 3)
for i := 0; i < 3; i++ {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := 0; j < innerLen;j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}
[maxwell@oracle-db-19c go_by_examples]$ go run slices.go
emp: [ ]
set: [a b c]
get: c
len: 3
apd [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d: [[0] [1 2] [2 3 4]]
[maxwell@oracle-db-19c go_by_examples]$
10. Maps
Maps are Go's built-in associate data type(sometimes called hashes or dicts in other languages)
To create an empty map,use the builtin make:
make(map[key-type]val-type)
Set key/value pairs using typical name[key] = val syntax
Printing a map with fmt.Println will show all of its key/value pairs.
Get a value for a key with name[key]
[maxwell@oracle-db-19c go_by_examples]$ vim maps.go
[maxwell@oracle-db-19c go_by_examples]$
[maxwell@oracle-db-19c go_by_examples]$ cat maps.go
package main
import "fmt"
func main() {
m := make(map[string]int)
m["k1"] = 7
m["k2"] = 13
fmt.Println("map:", m)
v1 := m["k1"]
fmt.Println("v1: ", v1)
fmt.Println("len:", len(m))
delete(m, "k2")
fmt.Println("map:", m)
_, prs := m["k2"]
fmt.Println("prs:", prs)
n := map[string]int{"foo": 1, "bar": 2}
fmt.Println("map:", n)
}
[maxwell@oracle-db-19c go_by_examples]$ go run maps.go
map: map[k1:7 k2:13]
v1: 7
len: 2
map: map[k1:7]
prs: false
map: map[bar:2 foo:1]
[maxwell@oracle-db-19c go_by_examples]$
170

被折叠的 条评论
为什么被折叠?



