douzengjian1535 2015-09-23 06:39 采纳率: 100%
浏览 16
已采纳

在[]字符串中分割值

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.

  • 写回答

2条回答 默认 最新

  • dqf35839 2015-09-23 06:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?