加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[日常] Go语言圣经-命令行参数

发布时间:2020-12-16 19:35:42 所属栏目:大数据 来源:网络整理
导读:1. go build hello.go 2.go get gopl.io/ch1/helloworld $GOPATH/src/gopl.io/ch1/helloworld 3.Go 4.Go gofmt -w hello.go 5.goimports go get golang.org/x/tools/cmd/goimports ? 命令行参数: 1.os 2. a = [1,2,3,4,5],a[0:3] = [1,3] 3.os.Args ??os.Ar

1.

go build hello.go

2.go get gopl.io/ch1/helloworld

$GOPATH/src/gopl.io/ch1/helloworld

3.Go

4.Go

gofmt -w hello.go

5.goimports

go get golang.org/x/tools/cmd/goimports ?

命令行参数:

1.os

2.

a = [1,2,3,4,5],a[0:3] = [1,3]

3.os.Args ??os.Args[1:]

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: 修改echo程序,使其能够打印os.Args[0],即被执行命令本身的名字
    s1,sep1:="",""
    for i := 0; i < len(os.Args); i++ {
            s1 += sep1 + os.Args[i]
            sep1 = " "
    }          
    fmt.Println(s1)
    //输出 /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函数
    fmt.Println(strings.Join(os.Args[0:]," "))

    //只是为了调试可以直接打印
    fmt.Println(os.Args[1:])

    //练习 1.2: 修改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)
    //输出 0 tsh 1 niu 2 bi

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读