go语言的循环语句for

栏目: Go · 发布时间: 6年前

内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/84547716

版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/84547716

一般来说,每种语言的循环语句都有两三种,什么for,foreach,while,do—until之类,应有尽有,总有一款适合您。但 go 是一款比较新的语言,语言的作者可能有一些自己的想法,循环语句就只提供了for一种。

suffixs := [6]string{".js",".css",".png",".jpg",".gif",".html"}
	for i := 0; i < len(suffixs); i++ {
		fmt.Println(suffixs[i])
	}

并且这个for是将foreach合而为一的。其他语言,像C#,foreach是很方便,但如果又想用到序号,就要在外面声明一个int i;然后循环体里i++之类,讨厌得很。但go版本的就挺好,可以将每轮迭代的序号和元素返回:

suffixs := [6]string{".js",".css",".png",".jpg",".gif",".html"}
	for i,sf := range suffixs{
		fmt.Printf("序号:%d,元素:%s\n\r",i,sf)	//注意%是写在前面的
	}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Twenty Lectures on Algorithmic Game Theory

Twenty Lectures on Algorithmic Game Theory

Tim Roughgarden / Cambridge University Press / 2016-8-31 / USD 34.99

Computer science and economics have engaged in a lively interaction over the past fifteen years, resulting in the new field of algorithmic game theory. Many problems that are central to modern compute......一起来看看 《Twenty Lectures on Algorithmic Game Theory》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具