To read a JSON file in Go, you can use the encoding/json
package to unmarshal the file's contents into a struct. Here's an example:
main.go579 chars33 lines
In this example, we define a Person
struct with Name
and Age
fields. We then read the contents of the file.json
file using the os.Open
function, and unmarshal the JSON data into a Person
struct using json.NewDecoder(file).Decode(&person)
.
We can then print out the person's name and age using fmt.Printf
.
gistlibby LogSnag