大佬教程收集整理的这篇文章主要介绍了[日常] Go语言圣经-命令行参数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1.
go build Hello.go
2.go get gopl.io/ch1/Helloworld 获取代码,并放到对应目录中
代码会放在$GOPATH/src/gopl.io/ch1/Helloworld
3.Go添加分号,除非一行上有多条语句。
4.Go代码格式上采取了很强硬的态度。gofmt代码格式化为标准格式
go get golang.org/x/tools/cmd/goimports
命令行参数:
2.获取子序列,序列的元素数目为
a = [1,2,3,4,5],a[0:3] = [1,3]
4.import
5.Go
for initialization; condition; post {
}
for condition {
}
for {//
}
import (
"fmt"
"os"
)
func main() {
//定义两个String类型的变量
var s,sep String
//短变量声明i
for i := 1; i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s)
//练习 1.1: <a href="http://code.js-code.com/tag/xiugai/" target="_blank" class="keywords">修改</a>echo程序,使其能够打印os.Args[0],即被执行命令本身的名字
s1,sep1:="",""
for i := 0; i < len(os.Args); i++ {
s1 += sep1 + os.Args[i]
sep1 = " "
}
fmt.Println(s1)
//<a href="http://code.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a> /tmp/go-build124823637/command-line-arguments/_obj/exe/echo1 tsh ni
u bi
}
6.for
range
_
7.函数
8.
fmt.Println(os.Args[1:])
import (
"fmt"
"os"
"Strings"
)
func main() {
s,sep := "","" //短变量声明
//循环迭代,range产生一对值;索引以及在该索引处的元素值。
//空标识符丢弃索引,arg是索引所对应的值
for ,arg := range os.Args[1:] {
s += sep + arg
sep = " "
}
fmt.Println(s)
//使用Strings包的Join<a href="http://code.js-code.com/tag/hanshu/" target="_blank" class="keywords">函数</a>
fmt.Println(Strings.Join(os.Args[0:]," "))
//只是为了调试可以直接打印
fmt.Println(os.Args[1:])
//练习 1.2: <a href="http://code.js-code.com/tag/xiugai/" target="_blank" class="keywords">修改</a>echo程序,使其打印每个参数的索引和值,每个一行。
s1 := ""
sep1 := "\n"
for index,arg := range os.Args[1:] {
index := fmt.Sprintf("%d ",indeX) //int转String
s1 += index + arg
s1 += sep1
}
fmt.Println(s1)
//<a href="http://code.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a> 0 tsh 1 niu 2 bi
}
以上是大佬教程为你收集整理的[日常] Go语言圣经-命令行参数全部内容,希望文章能够帮你解决[日常] Go语言圣经-命令行参数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。