codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

29 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Net Package
🐹Go (Golang)

Net Package

Updated 2026-05-15
10 min read

Net Package

Introduction

The net package in Go is a fundamental part of the standard library that provides low-level networking primitives. It includes functionalities for TCP, UDP, IP, Unix domain sockets, and more. Whether you're building a simple client-server application or a complex network service, understanding how to use the net package is crucial.

Concept

The net package offers several key features:

  1. TCP/IP: Functions for creating TCP and UDP connections.
  2. Listeners: Interfaces for accepting incoming connections.
  3. Dialing: Establishing connections to remote addresses.
  4. DNS Resolution: Resolving domain names to IP addresses.
  5. Interfaces: Accessing network interfaces.

In this tutorial, we'll explore some practical examples to help you understand how to use these features effectively.

Examples

1. TCP Client and Server

Let's start by creating a simple TCP client and server using the net package.

Server Example

package main

import (
	"bufio"
	"fmt"
	"net"
)

func handleConnection(conn net.Conn) {
	defer conn.Close()
	fmt.Println("Connected to:", conn.RemoteAddr())

	for {
		message, err := bufio.NewReader(conn).ReadString('\n')
		if err != nil {
			fmt.Println("Error reading:", err.Error())
			return
		}
		fmt.Print("Received message: ", string(message))
	}
}

func main() {
	listener, err := net.Listen("tcp", "localhost:8080")
	if err != nil {
		fmt.Println("Error listening:", err.Error())
		return
	}
	defer listener.Close()
	fmt.Println("Listening on localhost:8080")

	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("Error accepting connection:", err.Error())
			continue
		}
		go handleConnection(conn)
	}
}

#### Client Example

```go
package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:8080")
	if err != nil {
		fmt.Println("Error connecting:", err.Error())
		return
	}
	defer conn.Close()

	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Enter message: ")
		text, _ := reader.ReadString('\n')
		conn.Write([]byte(text))
	}
}

### 2. UDP Client and Server

Now, let's create a simple UDP client and server.

#### Server Example

```go
package main

import (
	"fmt"
	"net"
)

func handleConnection(conn *net.UDPConn) {
	buffer := make([]byte, 1024)
	for {
		n, addr, err := conn.ReadFromUDP(buffer)
		if err != nil {
			fmt.Println("Error reading:", err.Error())
			return
		}
		fmt.Printf("Received message: %s from %v\n", string(buffer[:n]), addr)

		conn.WriteToUDP([]byte("Message received!"), addr)
	}
}

func main() {
	addr, err := net.ResolveUDPAddr("udp", "localhost:8081")
	if err != nil {
		fmt.Println("Error resolving address:", err.Error())
		return
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		fmt.Println("Error listening:", err.Error())
		return
	}
	defer conn.Close()
	fmt.Println("Listening on localhost:8081")

	handleConnection(conn)
}

#### Client Example

```go
package main

import (
	"fmt"
	"net"
)

func main() {
	addr, err := net.ResolveUDPAddr("udp", "localhost:8081")
	if err != nil {
		fmt.Println("Error resolving address:", err.Error())
		return
	}

	conn, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		fmt.Println("Error connecting:", err.Error())
		return
	}
	defer conn.Close()

	message := []byte("Hello UDP!")
	_, err = conn.Write(message)
	if err != nil {
		fmt.Println("Error writing:", err.Error())
		return
	}

	buffer := make([]byte, 1024)
	n, _, err := conn.ReadFromUDP(buffer)
	if err != nil {
		fmt.Println("Error reading response:", err.Error())
		return
	}
	fmt.Printf("Received response: %s\n", string(buffer[:n]))
}

What's Next?

In the next section, we'll explore the http package, which is built on top of the net package and provides a higher-level API for HTTP clients and servers.

By understanding these basic networking capabilities with the net package, you'll be well-equipped to build more complex network applications in Go.


PreviousIO PackageNext HTTP Package

Recommended Gear

IO PackageHTTP Package