golang编程之获取命令行参数及环境变量

19710阅读 0评论2013-11-14 Bean_lee
分类:LINUX

    我们写习惯了C代码,都知道了解析输入参数argc argv,获取环境变量env,常见的C语言main函数有:
  1.     int main(int argc,char* argv[],char** envp)
    我们首先给出C语言的获取命令行参数和环境变量的代码:  
  1. manu@manu-hacks:~/code/c/self/env$ cat env.c

  2. #include<stdio.h>
  3. #include<stdlib.h>


  4. int main(int argc,char* argv[],char** envp)
  5. {
  6.     int i;
  7.     printf("there are %d input param\n",argc);
  8.     printf("they are :\n");
  9.     for(i = 0 ; i < argc ; i++)
  10.     {
  11.         printf("%s\n",argv[i]);
  12.     }
  13.     
  14.     printf("env LIST--------------------------------------\n");
  15.     char **p = envp;
  16.     for(;*p != NULL;p++)
  17.     {
  18.         printf("%s\n",*p);
  19.     }
  20.     return 0;
  21. }
    golang自然也可以做到,获取命令行参数,获取环境变量。
  1. manu@manu-hacks:~/code/go/self$ godoc os Args
  2. PACKAGE DOCUMENTATION

  3. package os
  4.     import "os"



  5. VARIABLES

  6. var Args []string
  7.     Args hold the command-line arguments, starting with the program name.
    我们先写一个简单的获取命令行参数的go 程序:   
  1. manu@manu-hacks:~/code/go/self$ cat sum.go
  2. package main
  3. import "fmt"
  4. import "os"
  5. import "strconv"


  6. func main() int{
  7.     arg_num := len(os.Args)
  8.     fmt.Printf("the num of input is %d\n",arg_num)

  9.     fmt.Printf("they are :\n")
  10.     for i := 0 ; i < arg_num ;i++{
  11.         fmt.Println(os.Args[i])
  12.     }

  13.     sum := 0
  14.     for i := 1 ; i < arg_num; i++{
  15.         curr,err := strconv.Atoi(os.Args[i])
  16.         if(err != nil){
  17.             fmt.Println("error happened ,exit")
  18.             return 1 
  19.         }
  20.         sum += curr
  21.     }

  22.     fmt.Printf("sum of Args is %d\n",sum)
  23.     return 0
  24. }
输出如下:
  1. manu@manu-hacks:~/code/go/self$ ./sum 1 2 4
  2. the num of input is 4
  3. they are :
  4. ./sum
  5. 1
  6. 2
  7. 4
  8. sum of Args is 7
  9. manu@manu-hacks:~/code/go/self$ ./sum 1 2 4 f 5
  10. the num of input is 6
  11. they are :
  12. ./sum
  13. 1
  14. 2
  15. 4
  16. f
  17. 5
  18. error happened ,exit
    另外一个方面是如何获取环境变量。对于C而言,直接就是 之际而言os中也有获取环境变量的函数:
  1. FUNCTIONS

  2. func Environ() []string
  3.     Environ returns a copy of strings representing the environment, in the
  4.     form "key=value".

  5. func Getenv(key string) string
  6.     Getenv retrieves the value of the environment variable named by the key.
  7.     It returns the value, which will be empty if the variable is not
  8.     present.
    有了这个,就简单了。写了个简单的例子:  
  1. manu@manu-hacks:~/code/go/self$ cat env.go
  2. package main
  3. import "fmt"
  4. import "os"

  5. func main(){

  6.     environ := os.Environ()
  7.     for i := range environ {
  8.         fmt.Println(environ[i])
  9.     }
  10.     fmt.Println("------------------------------------------------------------\n")
  11.     logname := os.Getenv("LOGNAME")
  12.     fmt.Printf("logname is %s\n",logname)
  13. }
    输出如下:
  1. manu@manu-hacks:~/code/go/self$ go run env.go
  2. SSH_AGENT_PID=2331
  3. GPG_AGENT_INFO=/tmp/keyring-5CkALe/gpg:0:1
  4. TERM=xterm
  5. SHELL=/bin/bash
  6. 。。。

  7. ------------------------------------------------------------

  8. logname is manu
参考文献
Package os

上一篇:golang 编程之fibonacci number
下一篇:golang编程之package flag 解析入参