go 学习1【基本语法】

直接上代码:

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")

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值