當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO IP.String用法及代碼示例

GO語言"net"包中"IP.String"類型的用法及代碼示例。

用法:

func(ip IP) String() string

String 返回 IP 地址 ip 的字符串形式。它返回 4 種形式之一:

- "<nil>", if ip has length 0
- dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address
- IPv6 conforming to RFC 5952 ("2001:db8::1"), if ip is a valid IPv6 address
- the hexadecimal form of ip, without punctuation, if no other cases apply

例子:

package main

import (
    "fmt"
    "net"
)

func main() {
    ipv6 := net.IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    ipv4 := net.IPv4(10, 255, 0, 0)

    fmt.Println(ipv6.String())
    fmt.Println(ipv4.String())

}

輸出:

fc00::
10.255.0.0

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 IP.String。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。