github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/net/netip/netip.go (about)

     1  //go:build js
     2  // +build js
     3  
     4  package netip
     5  
     6  type Addr struct {
     7  	addr uint128
     8  	// Unlike the upstream, we store the string directly instead of trying to
     9  	// use internal/intern package for optimization.
    10  	z string
    11  }
    12  
    13  var (
    14  	// Sentinel values for different zones. \x00 character makes it unlikely for
    15  	// the sentinel value to clash with any real-life IPv6 zone index.
    16  	z0    = ""
    17  	z4    = "\x00ipv4"
    18  	z6noz = "\x00ipv6noz"
    19  )
    20  
    21  func (ip Addr) Zone() string {
    22  	if ip.z == z4 || ip.z == z6noz {
    23  		return ""
    24  	}
    25  	return ip.z
    26  }
    27  
    28  func (ip Addr) WithZone(zone string) Addr {
    29  	if !ip.Is6() {
    30  		return ip
    31  	}
    32  	if zone == "" {
    33  		ip.z = z6noz
    34  		return ip
    35  	}
    36  	ip.z = zone
    37  	return ip
    38  }