这是我的代码,用于将字节数组传输到字符串 但它失败了:
window.onload = function() {
GetAllProperties();
};
程序不会生成预期输出
package main
import (
"bytes"
"fmt"
)
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
s := [][]byte{ip[:]}
//fmt.Println(s)
sep := []byte(".")
return string(bytes.Join(s, sep))
}
func main() {
hosts := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
如何在Golang中将[4]字节{1,2,3,4}转换为“1.2.3.4”?
答案 0 :(得分:3)
您不需要定义自己的类型,因为已经net.IP实现了fmt.Stringer。
示例:
package main
import (
"fmt"
"net"
)
func main() {
ip := net.IP{127, 0, 0, 1}
fmt.Println(ip)
}
net.IP
是一个字节切片,所以你也可以这样做:
b := []byte{127, 0, 0, 1}
ip := net.IP(b)
fmt.Println(ip)
答案 1 :(得分:1)
1-使用net.IP
,如下工作示例代码:
package main
import "fmt"
import "net"
func main() {
hosts := map[string]net.IP{
"loopback ": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Println(name, ":", ip)
}
}
输出:
loopback : 127.0.0.1
googleDNS : 8.8.8.8
2-使用此:
func (ip IPAddr) String() string {
return strconv.Itoa(int(ip[0])) + "." +
strconv.Itoa(int(ip[1])) + "." +
strconv.Itoa(int(ip[2])) + "." +
strconv.Itoa(int(ip[3]))
}
就像这个工作示例代码:
package main
import "fmt"
import "strconv"
type IPAddr [4]byte
func (ip IPAddr) String() string {
return strconv.Itoa(int(ip[0])) + "." +
strconv.Itoa(int(ip[1])) + "." +
strconv.Itoa(int(ip[2])) + "." +
strconv.Itoa(int(ip[3]))
}
func main() {
hosts := map[string]IPAddr{
"loopback ": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Println(name, ":", ip)
}
}
输出:
loopback : 127.0.0.1
googleDNS : 8.8.8.8
3-使用strings.Trim(strings.Join(strings.Fields(fmt.Sprint(ip)), "."), "[]")
像这样的工作示例代码:
package main
import "fmt"
import "strings"
type IPAddr [4]byte
func (ip IPAddr) String() string {
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint([4]byte(ip))), "."), "[]")
}
func main() {
hosts := map[string]IPAddr{
"loopback ": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Println(name, ":", ip)
}
}
输出:
loopback : 127.0.0.1
googleDNS : 8.8.8.8
答案 2 :(得分:0)
这就是我的做法。
package main
import (
"fmt"
)
type IPAddr [4]byte
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", int(ip[0]), int(ip[1]), int(ip[3]), int(ip[3]))
}
func main() {
hosts := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
答案 3 :(得分:0)
请注意,net.IP
is a []byte
是implements String() string
,因此您只需执行以下操作:
net.IP{127, 0, 0, 1}.String() // => "127.0.0.1"
此策略比fmt.Sprintf(...)
或strings.Join(...)
快得多:
// ip_string_test.go
var ip = net.IP{127, 0, 0, 1}
func Benchmark_net_IP_String(b *testing.B) {
for i := 0; i < b.N; i++ {
ip.String()
}
}
func Benchmark_fmt_Sprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
}
func Benchmark_strings_Join(b *testing.B) {
ss := make([]string, len(ip))
for i := 0; i < b.N; i++ {
for i, x := range ip {
ss[i] = strconv.FormatInt(int64(x), 10)
}
strings.Join(ss, ".")
}
}
$ go test -bench=. ./ip_string_test.go
goos: darwin
goarch: amd64
Benchmark_net_IP_String-8 50000000 30.8 ns/op
Benchmark_fmt_Sprintf-8 10000000 194 ns/op
Benchmark_strings_Join-8 20000000 113 ns/op
PASS
ok command-line-arguments 6.123s