packagemain import"fmt" import"os" import"strconv" funcmain()int{ arg_num:=len(os.Args) fmt.Printf("thenumofinputis%d\n",arg_num) fmt.Printf("theyare:\n") fori:=0;i<arg_num;i++{ fmt.Println(os.Args[i]) } sum:=0 fori:=1;i<arg_num;i++{ curr,err:=strconv.Atoi(os.Args[i]) if(err!=nil){ fmt.Println("errorhappened,exit") return1 } sum+=curr } fmt.Printf("sumofArgsis%d\n",sum) return0 }
manu@manu-hacks:~/code/go/self$./sum124 thenumofinputis4 theyare: ./sum 1 2 4 sumofArgsis7 manu@manu-hacks:~/code/go/self$./sum124f5 thenumofinputis6 theyare: ./sum 1 2 4 f 5 errorhappened,exit
获取系统环境变量
packagemain import"fmt" import"os" funcmain(){ environ:=os.Environ() fori:=rangeenviron{ fmt.Println(environ[i]) } fmt.Println("------------------------------------------------------------\n") logname:=os.Getenv("LOGNAME") fmt.Printf("lognameis%s\n",logname) }
manu@manu-hacks:~/code/go/self$gorunenv.go SSH_AGENT_PID=2331 GPG_AGENT_INFO=/tmp/keyring-5CkALe/gpg:0:1 TERM=xterm SHELL=/bin/bash 。。。 ------------------------------------------------------------ lognameismanu原文链接:https://www.f2er.com/go/190252.html