例如,两个步骤,

 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) } 

输出:

 127.0.0.1 5432 

举一个例子,

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

输出:

 127.0.0.1 5432 <nil>