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
There's are multiple ways to split a string :
_
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)
Split based on struct :
_
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 you code like ServerDetail.Host
and ServerDetail.Port
If you don't want to split specific string do it like this:
type ServerDetail struct {
Host string
Port string
}
ServerDetail = strings.Split([Your_String], ":") // Common split method
and use like ServerDetail.Host
and ServerDetail.Port
.
That's All.