How to split a string and assign it to variables

前端 未结 9 1537
无人及你
无人及你 2020-12-07 10:27

In Python it is possible to split a string and assign it to variables:

ip, port = \'127.0.0.1:5432\'.split(\':\')

but in Go it does not see

9条回答
  •  感情败类
    2020-12-07 11:27

    What you are doing is, you are accepting split response in two different variables, and strings.Split() is returning only one response and that is an array of string. you need to store it to single variable and then you can extract the part of string by fetching the index value of an array.

    example :

     var hostAndPort string
        hostAndPort = "127.0.0.1:8080"
        sArray := strings.Split(hostAndPort, ":")
        fmt.Println("host : " + sArray[0])
        fmt.Println("port : " + sArray[1])
    

提交回复
热议问题