Standard library / net/netip

netip.PrefixFrom()

Construct a prefix without clearing host bits; call Masked for canonical form. Expected stdout: '192.0.2.7/24\n'.

Formal Syntax & Structure

go syntaxgo
func PrefixFrom(ip Addr, bits int) Prefix

Parameters & Attributes

NameTypeDescription
ipAddrIP address to use or test.
bitsintNumber of leading network bits.

Return Value

Prefix

Operation result; check errors and validity indicators when returned.

Working Example

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