Standard library / net

net.IPNet.Contains()

Check whether an address belongs to the network. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (n *IPNet) Contains(ip IP) bool

Parameters & Attributes

NameTypeDescription
ipIPIP address to use or test.

Return Value

bool

Operation result; check errors and validity indicators when returned.

Working Example

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