Reputation: 79
I am a novice in Go programming language. I am writing a code to input from cli for client and pass the values to server for processing. both client and server are residing locally.
code :
package main
import (
"flag"
"fmt"
"strings"
)
func main() {
text := gettext()
fmt.Println(text)
result := strings.Split(text, " ")
for i := range result {
fmt.Println(result[i])
}
}
func gettext() []string {
flag.Parse()
text := flag.Args()
if len(text) < 1 {
fmt.Println("Please enter radius")
}
return text
}
when i run from command line its is giving me following error :cannot use text (type []string) as type string in argument to strings.Split
Basically i want to separately print values from []string .
Can you please let me know how to do it. I tried using strings.split.
Thanks.
Upvotes: 0
Views: 1447
Reputation: 190
strings.Split() is for spliting a string value, not a string array. Your gettext() is returing a string array, not a string. So you dont need string.Split(). Simply use
for _, str := range text {
fmt.Println(str)
}
Upvotes: 0
Reputation: 5686
You don't need to separate values by " "
The values already separated and your function gettext
returns a slice of parameters.
Try this example:
package main
import (
"flag"
"fmt"
)
func main() {
text := gettext()
fmt.Println(text)
for i := range text {
fmt.Println(text[i])
}
}
func gettext() []string {
flag.Parse()
text := flag.Args()
if len(text) < 1 {
fmt.Println("Please enter radius")
}
return text
}
From the documentation:
func Args() []string
Args
returns the non-flag command-line arguments.
Upvotes: 3