直接上代码:
package main
import (
"fmt"
"time"
"math"
"math/rand"
)
func plus5year( age int) int {
return age + 5
}
func main() {
// array
s := []int{1,2,3,4}
for i := 0 ; i < 10 ; i++ {
fmt.Printf("%d ", s[i])
if i == 3 {
fmt.Println("The length of s is", len(s));
break
}
}
// loop
sum := 1
for sum <= 100 {
sum += sum
}
fmt.Printf("The sum of 1 ~ 100 is %d\n", sum)
for i,value := range s {
if i == 2 {
fmt.Println("here i =", i, ".value is", value)
}
}
i := 0
for i<10 {
i++
fmt.Println(time.Now(),math.Pi,rand.Intn(3))
}
// switch
name := []string{"ChenLi","Nut"}
for i,value := range name {
switch name[i] {
case "ChengLi","ChenLi" :{
fmt.Println(name[i],value,"here")
}
default: {
fmt.Println("null")
}
case "Nut" :{
fmt.Println("Nut",value)
}
}
}
switch "nobody" {
case "ChengLi","ChenLi" :{
fmt.Println("here......")
}
default: {
fmt.Println("null.......")
}
case "Nut" :{
fmt.Println("Nut......")
}
}
// if
age := 20
plus5 := 0
if plus5 := plus5year(age); plus5 > 23 {
fmt.Println("You can get married whit the very person.")
}
plus5 += 100
fmt.Println(plus5)
fmt.Printf("The var in \"if plus5 := plus5year(age); plus5 > 23 {...}\"can not be used")
}