Learn Golang in Days - Day 11

2019-02-06  本文已影响3人  笑吧小鸟

Learn Golang in Days - Day 11

要点


实例


package main

import "fmt"


func main() {
    // ---------------- 定义切片 ----------------
    //定义slice
    nums := []int {1,2,3}
    sum := 0

    for _,num := range nums {
        fmt.Printf("%d,", num)
        sum += num
    }

    fmt.Printf("\nsum= %d\n",sum)
    fmt.Println()

    for i,num := range nums {
        fmt.Printf("nums[%d]=%d\n",i, num)
        sum += num
    }


    // ------------------ Map -------------------
    kvs := map[string]string{"a":"apple","b":"banana"}
    for k,v := range kvs {
        fmt.Printf("%s -> %s\n", k,v)   
    }

    //----------------- String ------------------
    s := "This is a tiger"
    for i,c := range s {
        fmt.Printf("[%d]=%c\n",i,c) 
    }
}
上一篇下一篇

猜你喜欢

热点阅读