Answers:
例如两个步骤
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>
err
遗憾,我得到了回复:too many colons in address 2001:0db8:85a3:0000:0000:8a2e:0370:7334
:(
由于go
是灵活的,您可以创建自己的python
样式拆分...
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
}
像RemoteAddr
from http.Request
这样的字段的IPv6地址格式为“ [:: 1]:53343”
因此net.SplitHostPort
效果很好:
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)
}
输出为:
127.0.0.1 5432 <nil>
::1 2345 <nil>
localhost 1234 <nil>
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)
}
这是字符串的定义。
// 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) }
分割字符串有多种方法:
_
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)
根据struct拆分:
_
type ServerDetail struct {
Host string
Port string
err error
}
ServerDetail = net.SplitHostPort("0.0.0.1:8080") //Specific for Host and Port
现在在您的代码中使用ServerDetail.Host
和ServerDetail.Port
如果您不想拆分特定的字符串,请按照以下步骤操作:
type ServerDetail struct {
Host string
Port string
}
ServerDetail = strings.Split([Your_String], ":") // Common split method
并使用like ServerDetail.Host
和ServerDetail.Port
。
就这样。
./prog.go:21:4: assignment mismatch: 1 variable but net.SplitHostPort returns 3 values
您正在做的是,您在两个不同的变量中接受拆分响应,而string.Split()仅返回一个响应,即一个字符串数组。您需要将其存储到单个变量,然后可以通过获取数组的索引值来提取字符串的一部分。
例如:
var hostAndPort string
hostAndPort = "127.0.0.1:8080"
sArray := strings.Split(hostAndPort, ":")
fmt.Println("host : " + sArray[0])
fmt.Println("port : " + sArray[1])
splittedString
:=strings.Split("127.0.0.1:5432", ":")
Ans:=splittedString[index]
您可以访问