How to split a string and assign it to variables in Golang?

In Python, you can split a string and assign it to variables:

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

but in Golang it does not work:

 ip, port := strings.Split("127.0.0.1:5432", ":") // assignment count mismatch: 2 = 1 

Question: How to split a line and assign values ​​in one step?

+130
string split go
May 14 '13 at 19:31
source share
7 answers

Two steps for example

 package main import ( "fmt" "strings" ) func main() { s := strings.Split("127.0.0.1:5432", ":") ip, port := s[0], s[1] fmt.Println(ip, port) } 

Output:

 127.0.0.1 5432 

One step for example

 package main import ( "fmt" "net" ) func main() { host, port, err := net.SplitHostPort("127.0.0.1:5432") fmt.Println(host, port, err) } 

Output:

 127.0.0.1 5432 <nil> 
+209
May 14 '13 at 19:45
source share

Since go is flexible, you can create your own python style ...

 package main import ( "fmt" "strings" "errors" ) type PyString string func main() { var py PyString py = "127.0.0.1:5432" ip, port , err := py.Split(":") // Python Style fmt.Println(ip, port, err) } func (py PyString) Split(str string) ( string, string , error ) { s := strings.Split(string(py), str) if len(s) < 2 { return "" , "", errors.New("Minimum match not found") } return s[0] , s[1] , nil } 
+20
Aug 02 '13 at
source share

IPv6 addresses for fields such as RemoteAddr from http.Request are in the format "[:: 1]: 53343"

So net.SplitHostPort works great:

 package main import ( "fmt" "net" ) func main() { host1, port, err := net.SplitHostPort("127.0.0.1:5432") fmt.Println(host1, port, err) host2, port, err := net.SplitHostPort("[::1]:2345") fmt.Println(host2, port, err) host3, port, err := net.SplitHostPort("localhost:1234") fmt.Println(host3, port, err) } 

Exit:

 127.0.0.1 5432 <nil> ::1 2345 <nil> localhost 1234 <nil> 
+5
Feb 25 '18 at 1:14
source share

There are several ways to split the string:

  1. If you want to make it temporary, split like this:

_

 import net package host, port, err := net.SplitHostPort("0.0.0.1:8080") if err != nil { fmt.Println("Error is splitting : "+err.error()); //do you code here } fmt.Println(host, port) 
  1. Divide based on structure:

    • Create a structure and split it like this

_

 type ServerDetail struct { Host string Port string err error } ServerDetail = net.SplitHostPort("0.0.0.1:8080") //Specific for Host and Port 

Now use in ServerDetail.Host code as ServerDetail.Host and ServerDetail.Port

If you do not want to split a specific line, follow these steps:

 type ServerDetail struct { Host string Port string } ServerDetail = strings.Split([Your_String], ":") // Common split method 

and use both ServerDetail.Host and ServerDetail.Port .

All this.

+1
Sep 21 '18 at 10:56
source share
 package main import ( "fmt" "strings" ) func main() { strs := strings.Split("127.0.0.1:5432", ":") ip := strs[0] port := strs[1] fmt.Println(ip, port) } 

Here is the definition for the strings.

 // Split slices s into all substrings separated by sep and returns a slice of // the substrings between those separators. // // If s does not contain sep and sep is not empty, Split returns a // slice of length 1 whose only element is s. // // If sep is empty, Split splits after each UTF-8 sequence. If both s // and sep are empty, Split returns an empty slice. // // It is equivalent to SplitN with a count of -1. func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) } 
+1
Dec 05 '18 at 8:46
source share

What you do, you accept a split answer in two different variables, and strings.Split () returns only one answer, and this is an array of strings. you need to save it in one variable, and then you can extract part of the string by extracting the index value of the 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]) 
0
Mar 21 '19 at 9:55 am
source share

Golang does not support implicit fragment unpacking (unlike python), and for this reason this will not work. As in the examples above, we have to get around this.

Note to one side:

Implicit unpacking occurs for variable functions in go:

func varParamFunc (params ... int) {

}

varParamFunc (slice1 ...)

0
Jun 11 '19 at 4:42
source share



All Articles