从Golang自定义结构中打印值

huangapple go评论69阅读模式
英文:

Print value from Golang custom strcu

问题

我是新手,对Golang不太熟悉。我有以下代码,请问有人可以帮忙从自定义创建的结构体中打印出ID吗?

type Currency struct {
	ID        uint         `db:"id" json:"id"`
	Name      string       `db:"name" json:"name"`
	Code      string       `db:"code" json:"code"`
	CreatedAt sql.NullTime `db:"createdAt" json:"createdAt"`
	UpdatedAt sql.NullTime `db:"updatedAt" json:"updatedAt"`
}

type temp struct {
	Currency map[string]Currency
}

func GetCurrencies(db *sqlx.DB) ([]Currency, error) {
	sqlStatement := `SELECT * FROM "Currencies"`
	var currencyRows []Currency
	err := db.Select(&currencyRows, sqlStatement)
	return currencyRows, err
}

func PrintC(db *sqlx.DB) {
	Currencies, _ := GetCurrencies(db)
	stores := []temp{}
	for _, currency := range Currencies {
		store := temp{
			Currency: map[string]Currency{
				currency.Name: {
					ID:        currency.ID,
					Name:      currency.Name,
					Code:      currency.Code,
					CreatedAt: currency.CreatedAt,
					UpdatedAt: currency.UpdatedAt,
				},
			},
		}
		stores = append(stores, store)
	}

	fmt.Println(stores)
}

当前输出:

[{map[Ethereum:{2 Ethereum ETH {2022-02-03 14:10:58.264 +0000 UTC true} {2022-02-03 14:10:58.264 +0000 UTC true}}]} {map[Bitcoin:{1 Bitcoin BTC {2022-02-03 14:10:59.471 +0000 UTC true} {2022-02-03 14:10:59.471 +0000 UTC true}}]}]

期望输出:

// 仅打印ETHEREUM的ID
// 仅打印BITCOIN的ID

英文:

I am new to Golang. I have below code,
Can someone please help to Print Only ID from the custom created struct?

type Currency struct {
	ID        uint         `db:"id" json:"id"`
	Name      string       `db:"name" json:"name"`
	Code      string       `db:"code" json:"code"`
	CreatedAt sql.NullTime `db:"createdAt" json:"createdAt"`
	UpdatedAt sql.NullTime `db:"updatedAt" json:"updatedAt"`
}

type temp struct {
	Currency map[string]Currency
}

func GetCurrencies(db *sqlx.DB) ([]Currency, error) {
	sqlStatement := `SELECT * FROM "Currencies"`
	var currencyRows []Currency
	err := db.Select(&currencyRows, sqlStatement)
	return currencyRows, err
}

func PrintC(db *sqlx.DB) {

	Currencies, _ := GetCurrencies(db)
	stores := []temp{}
	for _, currency := range Currencies {
		store := temp{
			Currency: map[string]Currency{
				currency.Name: {
					ID:        currency.ID,
					Name:      currency.Name,
					Code:      currency.Code,
					CreatedAt: currency.CreatedAt,
					UpdatedAt: currency.UpdatedAt,
				},
			},
		}
		stores = append(stores, store)
	}

  fmt.Println(stores)

}

Current Output:

[{map[Ethereum:{2 Ethereum ETH {2022-02-03 14:10:58.264 +0000 UTC true} {2022-02-03 14:10:58.264 +0000 UTC true}}]} {map[Bitcoin:{1 Bitcoin BTC {2022-02-03 14:10:59.471 +0000 UTC true} {2022-02-03 14:10:59.471 +0000 UTC true}}]}]

Expected Output:

// PRINT ONLY ETHEREUM ID
// PRINT ONLY BITCOIN ID

答案1

得分: 1

你可以实现Stringer接口,并定义你希望如何打印出你的结构体。
https://go.dev/tour/methods/17

英文:

You can implement Stringer interface and define the way how would you like to print out your struct.
https://go.dev/tour/methods/17

答案2

得分: 0

这段代码实现了Stringer接口,该接口是fmt包所需的。基本上,fmt包会查找这个接口来打印值。

func (cr Currency) String() string {
	return fmt.Sprintf("%v", cr.ID)
}
英文:

This snippet bellow implements the Stringer interface which is required by the fmt package. Basically the fmt package look for this interface to print values.

func (cr Currency) String() string {
	return fmt.Sprintf("%v", cr.ID)
}

huangapple
  • 本文由 发表于 2022年2月4日 07:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/70979537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定