3 回答
TA贡献1872条经验 获得超3个赞
如果您知道您的数据将是什么,您可以创建一个结构:例如
type User struct {
Username string `json:"username"`
Email string `json:"email"`
}
然后在您的查询中:
user := User{}
err = db.QueryRow("select colA, colB from table where colA = %v", 1).Scan(&user.Username,&user.Email)
if err != nil {
fmt.Printf(err.Error())
}
然后调用 marshal 就可以了
msg, err := json.Marshal(user)
if err != nil {
log.Println(err)
}
fmt.Println(string(msg)) // "{ "username": "Blah", "email": "asd@asd.com" }"
另外随着json.Marshal包,如果你调用Marshal在struct你的领域必须使用的字段名,例如首都首字母出口Username
// 此包忽略字段。字段整数json:"-"
// 字段在 JSON 中显示为键“myName”。字段整数json:"myName"
// 字段在 JSON 中显示为键 "myName" 并且 // 如果该字段的值为空,则该字段将从对象中省略, // 如上所述。字段整数 json:"myName,omitempty"
// 字段在 JSON 中显示为键“Field”(默认值),但 // 如果为空,则跳过该字段。// 注意前导逗号。字段整数 json:",omitempty"
https://golang.org/pkg/encoding/json/#Marshal
TA贡献1799条经验 获得超9个赞
您可以创建一个单元素映射并对其进行编组:
m := map[string]string{colA: colB}
b, err := json.Marshal(m)
完整示例:https : //play.golang.org/p/ntT5h3oHvY
TA贡献1805条经验 获得超9个赞
这是一个完整的示例:
campaign.model.go:
package models
import "database/sql"
// Campaign Model
type Campaign struct {
CampaignId int `db:"campaign_id" json:"campaignId"`
CampaignNme string `db:"campaign_nme" json:"campaignNme"`
OrganizationId sql.NullInt64 `db:"organization_id" json:"organizationId"`
}
campaign.repository.go:
package repositories
import (
"github.com/jmoiron/sqlx"
"go-clean-arch/domain/models"
"go-clean-arch/domain/repositories"
)
type CampaignRepository struct {
Db *sqlx.DB
}
func NewCampaignRepository(Db *sqlx.DB) repositories.CampaignRepository {
return &CampaignRepository{Db}
}
func (cr *CampaignRepository)FindById(id string) (models.Campaign, error) {
campaign := models.Campaign{}
query := `
SELECT
campaign_id,
campaign_nme,
organization_id
FROM "CAMPAIGN" WHERE campaign_id = $1
`
err := cr.Db.Get(&campaign, query, id)
if err != nil {
return campaign, err
}
return campaign, nil
}
main.go:
func main() {
dbConf := database.PGDatabaseConfig{
Host: viper.GetString("SQL_HOST"),
Port: viper.GetString("SQL_PORT"),
User: viper.GetString("SQL_USER"),
Password: viper.GetString("SQL_PASSWORD"),
Dbname: viper.GetString("SQL_DB"),
}
db, err := database.ConnectPGDatabase(&dbConf)
if err != nil {
log.Fatal(err)
}
defer func () {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
//userRepository := repositories.NewUserRepository(db)
//users, _ := userRepository.FindAll()
//fmt.Printf("%#v", &users)
campaignRepository := repositories.NewCampaignRepository(db)
campaign, err := campaignRepository.FindById("102")
if err != nil {
panic(err)
}
fmt.Printf("%#v", campaign)
campaignJSON, err := json.Marshal(campaign)
if err != nil {
panic(err)
}
fmt.Println(string(campaignJSON))
}
输出:
☁ go-clean-arch [master] ⚡ go run main.go
models.Campaign{CampaignId:102, CampaignNme:"Generic Party / Memories", OrganizationId:sql.NullInt64{Int64:0, Valid:false}}
campaignJSON: {"campaignId":102,"campaignNme":"Generic Party / Memories","organizationId":{"Int64":0,"Valid":false}}
- 3 回答
- 0 关注
- 321 浏览
添加回答
举报