Encoding a simple struct to JSON

Report a typo

Xavi has created a Go program that has the FootballPlayer struct that will hold specific data about different football players. It will take the data about players as input.

Now Xavi needs to encode to JSON format the FootballPlayer struct. Unfortunately, he is running late for his morning training session and needs your help to write the additional code to serialize and print the struct!

Below is the code Xavi has written so far:

Sample Input 1:

Ansu Fati 19 Barcelona Forward

Sample Output 1:

{"name":"Ansu","lastName":"Fati","age":19,"team":"Barcelona","position":"Forward"}
Write a program in Go
package main

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

// DO NOT delete or modify the FootballPlayer struct implementation:
type FootballPlayer struct {
Name string `json:"name"`
LastName string `json:"lastName"`
Age int `json:"age"`
Team string `json:"team"`
Position string `json:"position"`
}

func main() {
// DO NOT delete! - This code block takes as an input the values for the 'player' struct:
var name, lastName, team, position string
var age int
fmt.Scanln(&name, &lastName, &age, &team, &position)

player := FootballPlayer{
Name: name,
LastName: lastName,
Age: age,
Team: team,
Position: position,
}

// Write the code below to properly encode the 'player' struct into the 'playerJSON' variable:
?, err := ?.?(?)
if err != nil {
log.Fatal(err)
}
___

Create a free account to access the full topic