duanjiu1003 2016-04-27 19:43
浏览 139
已采纳

Golang中的参数处理

I will be giving the following as the command to run my go program.

go run app.go 3001-3005

What this is supposed to do is, run my go Rest api on server ports 3001 to 3005.

This part of my main function which handles this argument.

func main() { 
ipfile := os.Args[1:]
s := strings.Split(ipfile, "-")
mux := routes.New() 
mux.Put("/:key1/:value1", PutData)
mux.Get("/profile/:key1", GetSingleData) 
mux.Get("/profile", GetData)
http.Handle("/", mix)

Here I will run a for loop and replace the first argument with s[i].

http.ListenAndServe(":3000", nil)
}

I get the following output:

cannot use ipfile (type []string) as type string in argument to strings.Split

What datatype does os.args return? I tried converting it to string and then splitting. Does not work. Please let me know what is wrong?

  • 写回答

1条回答 默认 最新

  • dongsuichi6529 2016-04-27 19:46
    关注

    Like the error says, ipfile is a []string. The [1:] slice operation is going to return a slice, even if there's only 1 element.

    After checking that os.Args has the enough elements, use:

    ipfile := os.Args[1]
    s := strings.Split(ipfile, "-")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys