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) PrefixParameters & Attributes
| Name | Type | Description |
|---|---|---|
| ip | Addr | IP address to use or test. |
| bits | int | Number 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) }}