Muhammad Tariq
Muhammad Tariq

Reputation: 4634

Add bson tags to the go struct using GoModifyTags

I am trying to use GoModifyTags but it's not giving me required result.
I have installed this as per the instruction but don't know how to implement. Using VScode editor.

I am trying to add bson tags in my file.

Currently, I have:

type option []struct {
    Option  string  `json:"option"`
    ID      float64 `json:"id"`
    Correct bool    `json:"correct"`
}  

Required:

type option []struct {
    Option  string  `json:"option" bson:"option"`
    ID      float64 `json:"id" bson:"id"`
    Correct bool    `json:"correct" bson:"correct"`
}

How can I achieve this?

Solution:

Following the instruction of Jihoon Ye, I am able to get my required result. Here are steps for VSCode with pictures to help you understand better.

  1. Go to File -> Preference -> Settings

  2. You must have GO extension installed on your VSCode
    enter image description here

  3. Type "Go: Add Tags" in the search box and you will see the settings as below. Add bson in the tag field. enter image description here

  4. Select your go struct in the code

  5. Right-click the selected code OR use command palette (use shortcut cntrl + shift + p) and select "Go: Add tags to struct fields" enter image description here

  6. Here is the final result enter image description here

Peace,

Upvotes: 3

Views: 3270

Answers (1)

Jihoon Yeo
Jihoon Yeo

Reputation: 206

Using the command

install GoModifyTags.

$ go get github.com/fatih/gomodifytags

Enter the command below.

$ gomodifytags -file main.go -struct option -add-tags bson -w

The results below will then be printed out.

type option []struct {
    Option  string  `json:"option" bson:"option"`
    ID      float64 `json:"id" bson:"id"`
    Correct bool    `json:"correct" bson:"correct"`
}

Using the VSCode

In user settings, setting "Go: Add Tags" as below. enter image description here

Place the cursor in the structure and run "Go: Add Tags To Struct Field".

Upvotes: 2

Related Questions