How to have development struct and production struct in Golang with same members but different JSON tags?

WBOY
Release: 2024-02-11 21:54:08
forward
609 people have browsed it

如何在 Golang 中拥有具有相同成员但不同 JSON 标签的开发结构和生产结构?

php Editor Apple In Golang development, we often encounter situations where we need to use the same members in the development structure and production structure, but need different JSON tags. In this case, we need to find a flexible solution so that we can easily switch between different tabs while writing code. This article will introduce how to implement this requirement in Golang to make the development process more efficient and flexible.

Question content

First time asking! I'm trying to separate development and production using the same structure.

I'm using airtable which sends the records as json with thefldtags we use when unmarshalling.

type airtablerecord struct { name *string `json:"fldaaaa,omitempty"` }
Copy after login

I have 2 separate airtables:

  1. For development
  2. for production

They are the same, it's just that due to the way airtable works, the fields are given differentfldtags

Pictures of my airtable venue

Now to separate the development environment from the production environment, I have to uncomment the correct members based on the airtable I'm pointing to.

type airtablerecord struct { // development name *string `json:"fldaaaa,omitempty"` // production //name *string `json:"fldbbbb,omitempty"` }
Copy after login

I keep this type in it's own model.go file for use by other packages.

I have investigated:

  • There are multiple json tags in one line, golang will not do this
type airtablerecord struct { // development or production name *string `json:"fldaaaa,fldbbbb,omitempty"` }
Copy after login
  • Separating my files using build tags, maybe this works but I'm doing it wrong

File 1:

// +build dev type airtablerecord struct { // development name *string `json:"fldaaaa,omitempty"` }
Copy after login
Copy after login

File 2:

type AirtableRecord struct { // Production Name *string `json:"fldBBBB,omitempty"` }
Copy after login
  • Have looked into using re-tagging but the examples they gave don't look like what I'm looking for
    • Retag link: https://pkg.go.dev/github.com/sevlyar/[email protected]

I want to dynamically change the label of this member based on whether I'm running in development mode or production mode.

Any and all help would be greatly appreciated!

Workaround

If you receive aredeclaredcompilation error using build flags in this block, specify an unmarked flag on the prod file , to avoid this situation.

Development files

// +build dev type airtablerecord struct { // development name *string `json:"fldaaaa,omitempty"` }
Copy after login
Copy after login

Product Documents

// +build !dev type airtablerecord struct { // development name *string `json:"fldaaaa,omitempty"` }
Copy after login

Construct

# for dev go build -tags=dev -o devrel # for prod go build -tags=prod -o prodrel or no tags for prod
Copy after login

The build tag format has also changed since 1.17, so in your case it would be,

//go:build dev
Copy after login

But should also be used with the old one.

The above is the detailed content of How to have development struct and production struct in Golang with same members but different JSON tags?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!