When you write Go code, you’re not working in a vacuum. Real-world applications often need to talk to other systems, save and load data, or communicate over the internet. That’s where file I/O, JSON, and network protocols come in.
In this tutorial, we’ll cover:
- How Go handles files
- What JSON is and how to use it
- How Go communicates over protocols like HTTP and TCP/IP
Let’s jump in.
Communicating Beyond Your Program
Your Go program will often need to:
- Read/write from files
- Exchange data with APIs
- Send or receive messages over a network
To do this, we rely on standardized formats and protocols—rules that both sides understand. These include things like JSON, HTML, HTTP, and more. In Go, many of these are supported with built-in packages, so you don’t have to reinvent the wheel.
RFCs: The Blueprint of Communication
An RFC (Request for Comments) is basically a public standard. Think of it like a contract that says, “Here’s how data should be structured so both sides understand each other.”
For example:
- HTML – how web pages are structured
- HTTP (RFC 2616) – how web clients and servers talk
- URI – how resources are located on the web
- JSON (RFC 7159) – a universal format for structured data
Go supports many of these via standard packages, so working with them is usually just a matter of importing the right package.
Using Go Packages for Common Protocols
net/http
Use this when you want your program to make web requests or serve web content.
resp, err := http.Get("http://example.com")
net
Want to create low-level TCP/IP or socket connections? This package is your go-to.
conn, err := net.Dial("tcp", "example.com:80")
These libraries handle a lot of the complex protocol details for you, so you can focus on the logic.
JSON: The Universal Language of Data
JSON (JavaScript Object Notation) is one of the most popular data formats. It’s simple, readable, and maps well to Go’s structs and maps.
Let’s say you have a Person struct:
type Person struct {
Name string
Address string
Phone string
}
Now you create a person:
p := Person{Name: "Joe", Address: "123", Phone: "123"}
To convert this to JSONL:
barr, err := json.Marshal(p)
The result barr
is a []byte
representing this JSON:
{
"Name": "Joe",
"Address": "123",
"Phone": "123"
}
JSON to a Go struct: Now, let’s say you receive some JSON and want to convert it into a Go object:
var pp Person
err := json.Unmarshal(barr, &pp)
This populates pp with the data from the JSON.
-> Just make sure the struct field names match the JSON keys!
Working with Files in Go
Go makes file access straightforward but gives you the power to go deeper if needed.
Want to read part of a file? Use the os package:
f, err := os.Open("data.txt")
b := make([]byte, 10)
n, err := f.Read(b)
f.Close()
Need to write to a file?
f, err := os.Create("output.txt")
f.Write([]byte{1, 2, 3})
f.WriteString("Hi there!")
f.Close()
This lets you control how much you read/write, and when.
This knowledge is the foundation for building real-world applications that save files, talk to web services, or process external data.
In the next article we are going to work with functions
Top comments (0)