Preview
Open Original
Golang (or Go) is a modern programming language that is easy to install and perfect for handling common tasks like working with JSON. Here’s your quick guide:
Step 1: Install Golang
Windows
- Go to the official Go downloads page
- Download the Windows installer (
.msi) and run it - Follow the installation wizard and ensure Go is added to your system PATH
macOS
- Visit the Go official website and download the
.pkginstaller - Double-click the package and follow the installation instructions
Linux
- Download the installation package (replace
1.xx.xwith the latest version):
wget https://golang.org/dl/go1.xx.x.linux-amd64.tar.gz
- Extract to
/usr/local(may require sudo):
tar -C /usr/local -xzf go1.xx.x.l...
Golang (or Go) is a modern programming language that is easy to install and perfect for handling common tasks like working with JSON. Here’s your quick guide:
Step 1: Install Golang
Windows
- Go to the official Go downloads page
- Download the Windows installer (
.msi) and run it - Follow the installation wizard and ensure Go is added to your system PATH
macOS
- Visit the Go official website and download the
.pkginstaller - Double-click the package and follow the installation instructions
Linux
- Download the installation package (replace
1.xx.xwith the latest version):
wget https://golang.org/dl/go1.xx.x.linux-amd64.tar.gz
- Extract to
/usr/local(may require sudo):
tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz
- Add to your environment variables in
~/.bashrcor~/.zshrc:
export PATH=$PATH:/usr/local/go/bin
- Activate the environment:
source ~/.bashrc # or source ~/.zshrc
- Verify installation:
go version
Step 2: Verify Installation
In your terminal, type:
go version
If you see the Go version number, congratulations! Installation was successful.
Step 3: Handle JSON in Go
Go’s standard library includes the powerful encoding/json package. Here’s a simple example showing how to parse and generate JSON.
Example: Parse and Generate JSON
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
// JSON string
data := `{"name": "Alice", "age": 30}`
// Parse (Unmarshal) JSON string to struct
var p Person
err := json.Unmarshal([]byte(data), &p)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
fmt.Printf("Name: %s, Age: %d\n", p.Name, p.Age)
// Convert struct to JSON string (Marshal)
newData, err := json.Marshal(p)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
fmt.Println("JSON Output:", string(newData))
}
Output
Name: Alice, Age: 30
JSON Output: {"name":"Alice","age":30}
Summary
- Installing Go is straightforward - use official downloads or mirrors
- Go’s
encoding/jsonpackage makes JSON handling easy - The example above shows how to parse JSON to structs and generate JSON from structs
You can copy the code above to a local main.go file and run:
go run main.go
For further learning, check out the official Go tutorial.
Happy coding! 🚀