Update tech_docs/go_getting_started.md

This commit is contained in:
2024-06-18 05:05:10 +00:00
parent 78c77600e7
commit 8ef9f4fe62

View File

@@ -1,3 +1,266 @@
Go provides strong support for working with various file formats such as CSV, JSON, Markdown, and YAML. Let's dive into the specifics for each of these formats.
### CSV
#### Standard Library: `encoding/csv`
The `encoding/csv` package provides functions for reading and writing CSV files.
**Reading CSV Files:**
```go
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
file, err := os.Open("data.csv")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV:", err)
return
}
for _, record := range records {
fmt.Println(record)
}
}
```
**Writing CSV Files:**
```go
package main
import (
"encoding/csv"
"os"
)
func main() {
file, err := os.Create("output.csv")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
data := [][]string{
{"Name", "Age", "City"},
{"Alice", "30", "New York"},
{"Bob", "25", "San Francisco"},
}
for _, record := range data {
if err := writer.Write(record); err != nil {
fmt.Println("Error writing record:", err)
return
}
}
}
```
### JSON
#### Standard Library: `encoding/json`
The `encoding/json` package is used for encoding and decoding JSON data.
**Reading JSON Files:**
```go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
file, err := os.Open("data.json")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
var people []Person
json.Unmarshal(byteValue, &people)
for _, person := range people {
fmt.Printf("Name: %s, Age: %d\n", person.Name, person.Age)
}
}
```
**Writing JSON Files:**
```go
package main
import (
"encoding/json"
"fmt"
"os"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
people := []Person{
{"Alice", 30},
{"Bob", 25},
}
file, err := os.Create("output.json")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
if err := encoder.Encode(people); err != nil {
fmt.Println("Error encoding JSON:", err)
}
}
```
### Markdown
#### Third-Party Library: `gomarkdown/markdown`
Go does not have a built-in package for Markdown, but third-party libraries like `gomarkdown/markdown` can be used.
**Installing `gomarkdown/markdown`:**
```sh
go get github.com/gomarkdown/markdown
```
**Rendering Markdown to HTML:**
```go
package main
import (
"fmt"
"github.com/gomarkdown/markdown"
)
func main() {
markdownContent := []byte("# Hello, Markdown!\nThis is a simple markdown file.")
htmlContent := markdown.ToHTML(markdownContent, nil, nil)
fmt.Println(string(htmlContent))
}
```
### YAML
#### Third-Party Library: `go-yaml/yaml`
Go does not have a built-in package for YAML, but the `go-yaml/yaml` package is a popular choice.
**Installing `go-yaml/yaml`:**
```sh
go get gopkg.in/yaml.v2
```
**Reading YAML Files:**
```go
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type Config struct {
Version string `yaml:"version"`
AppName string `yaml:"app_name"`
}
func main() {
file, err := os.Open("config.yaml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
var config Config
yaml.Unmarshal(byteValue, &config)
fmt.Printf("Version: %s, AppName: %s\n", config.Version, config.AppName)
}
```
**Writing YAML Files:**
```go
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
)
type Config struct {
Version string `yaml:"version"`
AppName string `yaml:"app_name"`
}
func main() {
config := Config{
Version: "1.0",
AppName: "MyApp",
}
file, err := os.Create("output.yaml")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
data, err := yaml.Marshal(&config)
if err != nil {
fmt.Println("Error marshaling YAML:", err)
return
}
file.Write(data)
}
```
### Summary
Go provides strong native support for working with CSV and JSON through the `encoding/csv` and `encoding/json` packages, respectively. For Markdown and YAML, third-party libraries like `gomarkdown/markdown` and `go-yaml/yaml` are commonly used. These tools allow Go developers to efficiently handle these file formats, making Go a versatile choice for many data processing tasks.
---
## Deep Dive into Golang
### Language Design