Reputation: 405
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?
Upvotes: 0
Views: 473
Reputation: 109417
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, "-")
Upvotes: 4