da2298e8-32b9-429a-8bdf-9bec76e1ab52
prompter
Write an example code in golang to decode the json file
8cb5d9c4-13ca-4561-a113-49dde2752aad
da2298e8-32b9-429a-8bdf-9bec76e1ab52
assistant
Sure, here is an example code in Go to decode a JSON file:


```
package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type Person struct {
    Name  string   `json:"name"`
    Age   int      `json:"age"`
    Email []string `json:"email"`
}

func main() {
    file, err := os.Open("people.json")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file.Close()

    var people []Person

    decoder := json.NewDecoder(file)
    err = decoder.Decode(&people)
    if err != nil {
        fmt.Println("Error decoding JSON:", err)
        return
    }

    fmt.Println(people)
}
```

In this example, we define a struct Person to match the JSON data. Then, we open the people.json file and use a JSON decoder to read and decode the contents of the file into a slice of Person structs. Finally, we print the resulting slice of Person structs to the console. Note that we use the defer keyword to ensure that the file is closed after we're done reading from it.