AbhiSam
AbhiSam

Reputation: 105

How to convert simple struct in go with json structure

I am new to GO and I have one query with struct. I am want to convert below strut

type Dog struct {
  ID      int
  Name    string
  Breed   string
  BornAt  time.Time
}

to

type JSONDog struct {
  ID     int    `json:"id"`
  Name   string `json:"name"`
  Breed  string `json:"breed"`
  BornAt int64  `json:"born_at"`
}

is there anyway to automatically create it as if there are 100 of struct like that it will take long time manually.

Upvotes: 0

Views: 295

Answers (1)

tjarkmeyer
tjarkmeyer

Reputation: 121

If you have the JSON you can take your JSON and something like this online convert to get the struct:

https://transform.tools/json-to-go

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

will be

type AutoGenerated struct {
    UserID    int    `json:"userId"`
    ID        int    `json:"id"`
    Title     string `json:"title"`
    Completed bool   `json:"completed"`
}

Upvotes: 2

Related Questions