• 基础语法-循环使用(9)


    总体

    代码

    //demo_18.go
    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	person := [3] string {"Tom", "Aaron", "John"}
    	fmt.Printf("len=%d cap=%d array=%v
    ",len(person),cap(person),person)
    
    	fmt.Println("")
    
    	//循环
    	for k, v := range person {
    		fmt.Printf("person[%d]: %s
    ", k, v)
    	}
    
    	fmt.Println("")
    
    	for i := range person {
    		fmt.Printf("person[%d]: %s
    ", i, person[i])
    	}
    
    	fmt.Println("")
    
    	for i := 0; i < len(person); i++ {
    		fmt.Printf("person[%d]: %s
    ", i, person[i])
    	}
    
    	fmt.Println("")
    
    	//使用空白符
    	for _, name := range person {
    		fmt.Println("name :", name)
    	}
    }
    
    

    输出

    len=3 cap=3 array=[Tom Aaron John]
    
    person[0]: Tom
    person[1]: Aaron
    person[2]: John
    
    person[0]: Tom
    person[1]: Aaron
    person[2]: John
    
    person[0]: Tom
    person[1]: Aaron
    person[2]: John
    
    name : Tom
    name : Aaron
    name : John
    

    循环map

    //demo_20.go
    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	person := map[int]string{
    		1 : "Tom",
    		2 : "Aaron",
    		3 : "John",
    	}
    
    	fmt.Printf("len=%d map=%v
    ", len(person), person)
    
    	fmt.Println("")
    
    	//循环
    	for k, v := range person {
    		fmt.Printf("person[%d]: %s
    ", k, v)
    	}
    
    	fmt.Println("")
    
    	for i := range person {
    		fmt.Printf("person[%d]: %s
    ", i, person[i])
    	}
    
    	fmt.Println("")
    
    	for i := 1; i <= len(person); i++ {
    		fmt.Printf("person[%d]: %s
    ", i, person[i])
    	}
    
    	fmt.Println("")
    
    	//使用空白符
    	for _, name := range person {
    		fmt.Println("name :", name)
    	}
    }
    
    

    for

    for i := 1; i <= 10; i++ {
    		if i == 6 {
    			break//continue
    		}
    		fmt.Println("i =", i)
    	}
    
    
    func main() {
    	fmt.Println("begin")
    
    	for i := 1; i <= 10; i++ {
    		if i == 6 {
    			goto END
    		}
    		fmt.Println("i =", i)
    	}
    
    	END :
    		fmt.Println("end")
    }
    
    
  • 相关阅读:
    并行执行计划
    mongodb数据查询-小结
    mongodb的基本操作-小结
    架构
    bat、dos控制多个后台程序启动
    python技术实践清单
    Linux升级安装python2.7.5到python2.7.9
    数据分析-GDP统计
    技术能力清单-小结
    动态链接库*.so
  • 原文地址:https://www.cnblogs.com/icxldd/p/13777543.html
Copyright © 2020-2023  润新知