Standard library / net/netip

netip.AddrFromSlice()

Accept only 4 or 16 bytes; check the boolean before using the address. Expected stdout: '192.0.2.7 true\n'.

Formal Syntax & Structure

go syntaxgo
func AddrFromSlice(slice []byte) (ip Addr, ok bool)

Parameters & Attributes

NameTypeDescription
slice[]byteFour or sixteen address bytes.

Return Value

(ip Addr, ok bool)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/netip")
func main() { v, ok := netip.AddrFromSlice([]byte{192, 0, 2, 7}); fmt.Println(v, ok) }
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference