Standard library / net

net.ParseCIDR()

Parse the original address and its masked network as separate values. Expected stdout: '192.0.2.7 192.0.2.0/24\n'.

Formal Syntax & Structure

go syntaxgo
func ParseCIDR(s string) (IP, *IPNet, error)

Parameters & Attributes

NameTypeDescription
sstringTextual address or prefix to parse.

Return Value

(IP, *IPNet, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net")
func main() { ip, n, e := net.ParseCIDR("192.0.2.7/24"); check(e); fmt.Println(ip, n) }
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