Menu Close

Go – type IP in net package in go

Here, We will learn about type IP in net package in go. type IP provides multiple interfaces to play around IPv4 or IPv6 address.

An IP is a single IP address, a slice of bytes. Functions in type IP in net package in go accept either 4-byte (IPv4) or 16-byte (IPv6) slices as input .

Function prototype:

type IP []byte

Example with code:

package main

import (
	"fmt"
	"net"
)

func main() {
	ipv6 := net.IP{0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	ipv4 := net.IPv4(10, 10, 10, 0)

	fmt.Println(ipv6.To4())
	fmt.Println(ipv4.To4())

}

Output:

<nil>
10.10.10.0

Functions of type IP

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References: https://golang.org/pkg/

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *