Standard library / net/netip

netip.Addr.Prefix()

Keep the leading bits and clear host bits; invalid lengths return an error. Expected stdout: '192.0.2.0/24\n'.

Formal Syntax & Structure

go syntaxgo
func (ip Addr) Prefix(b int) (Prefix, error)

Parameters & Attributes

NameTypeDescription
bintByte buffer, binary representation or prefix length as indicated by its type.

Return Value

(Prefix, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/netip")
func main() { fmt.Println(must(netip.MustParseAddr("192.0.2.7").Prefix(24))) }
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