Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
communitycreator
cidrmask

What is the net.CIDRMask() function in Golang?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The CIDRMask() function of the net package in Golang returns an IPMask object that contains a certain number of 1 and 0 bits.

To use the CIDRMask() function, we first import the net package into our program as shown below:

import (
   "net"
)

Syntax

The syntax of the CIDRMask() function is shown below:

func CIDRMask(ones, bits int) IPMask

Parameters

The CIDRMask() function accepts the following parameters:

  • ones: This is the number of bits to be set to a value of 1 in the IPMask.
  • bits: This is the total length of the IPMask.

Return value

The CIDRMask() function returns an IPMask with a length equal to the bits parameter value.

The value of the ones parameter determines how many bits of the IPMask will have a value of 1. The remaining bits will be set to 0.

If the value of bits does not correspond to a valid subnet for IPv4 or IPv6, nil is returned.

Code

The code below shows how the CIDRMask() function works in Golang:

package main
// import necessary packages
import (
"net"
"fmt"
)
func main(){
// call the CIDRMask function for IPv4 masks
fmt.Println(net.CIDRMask(16, 32))
fmt.Println(net.CIDRMask(31, 32))
// call the CIDRMask function for an IPv6 mask
fmt.Println(net.CIDRMask(64, 128))
// call the function for an invalid length
fmt.Println(net.CIDRMask(20, 40))
}
Simple examples using the CIDRMask() function

Explanation

  • Lines 4-7: We import the net and fmt packages.
  • Line 12: We call the CIDRMask function with the values of ones and bits set to 16 and 32, respectively. Consequently, the function returns an IPMask of length 32 with exactly 16 bits set to 1.
  • Line 13: We call the CIDRMask function with the values of ones and bits set to 31 and 32, respectively. Consequently, the function returns an IPMask of length 32 with exactly 31 bits set to 1.
  • Line 16: We call the CIDRMask function for an IPv6 subnet with 64 bits to be set to 1. The function returns the corresponding IPMask.
  • Line 19: We call the CIDRMask function with a value of 40 for the bits parameter. Since a length of 40 does not correspond to a valid IPv4 or IPv6 address, the function returns nil.

RELATED TAGS

golang
communitycreator
cidrmask

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring