Jamers Posted May 13, 2019 Report Share Posted May 13, 2019 package main import ( "./myfunc" //加载本项目包 "flag" //解析命令行参数 "fmt" "os" "strconv" ) //定义解析的命令行参数 var s = flag.String("s", "", "某某字符串") var b = flag.Bool("b", false, "布尔参数") func main() { //定义names变量 等同于 names := "World" var names = "World" //遍历命令行参数并输出 for idx, args := range os.Args { fmt.Println(strconv.Itoa(idx) + ":", args) } //解析命令行参数并赋值 flag.Parse() fmt.Println("-b:", *b) fmt.Println("-s:", *s) //未解析的参数 var params = flag.Args() fmt.Println(params) //如果有未解析的参数,访问 if len(params) > 0 { names = params[0] } fmt.Println("Hello " + names + "!") //多数据定义并赋值 var a, b = 2, 3 //省略var的方式定义,如果之前已经有此变量时会产生编译错误 c := 99 fmt.Printf("%d _++_+ %d, %x\n", a, b, &c) // & 取地址 * 取内容 fmt.Println(*&c) //调用其他包的函数 fmt.Println(myfunc.Demo("Hello", "=-=-=", "World!")) //字典定义以及遍历 dict := map[string] string {"One": "1111", "Two": "2222", "Three": "3333",} for k, v := range dict { fmt.Printf("%s: %s\n", k, v) } } /* 文件名及路径:myfunc/functions.go */ package myfunc func Demo(sstr ...string) string { result := "" //数组遍历方式 /*for i := 0; i < len(sstr); i ++ { result += " " + sstr[i] }*/ //通用遍历方式 数组、切片、字典通用 for _, v := range sstr { result += " " + v } return result } Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now