从Golang自定义结构中打印值

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

Print value from Golang custom strcu

问题

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

  1. type Currency struct {
  2. ID uint `db:"id" json:"id"`
  3. Name string `db:"name" json:"name"`
  4. Code string `db:"code" json:"code"`
  5. CreatedAt sql.NullTime `db:"createdAt" json:"createdAt"`
  6. UpdatedAt sql.NullTime `db:"updatedAt" json:"updatedAt"`
  7. }
  8. type temp struct {
  9. Currency map[string]Currency
  10. }
  11. func GetCurrencies(db *sqlx.DB) ([]Currency, error) {
  12. sqlStatement := `SELECT * FROM "Currencies"`
  13. var currencyRows []Currency
  14. err := db.Select(&currencyRows, sqlStatement)
  15. return currencyRows, err
  16. }
  17. func PrintC(db *sqlx.DB) {
  18. Currencies, _ := GetCurrencies(db)
  19. stores := []temp{}
  20. for _, currency := range Currencies {
  21. store := temp{
  22. Currency: map[string]Currency{
  23. currency.Name: {
  24. ID: currency.ID,
  25. Name: currency.Name,
  26. Code: currency.Code,
  27. CreatedAt: currency.CreatedAt,
  28. UpdatedAt: currency.UpdatedAt,
  29. },
  30. },
  31. }
  32. stores = append(stores, store)
  33. }
  34. fmt.Println(stores)
  35. }

当前输出:

  1. [{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?

  1. type Currency struct {
  2. ID uint `db:"id" json:"id"`
  3. Name string `db:"name" json:"name"`
  4. Code string `db:"code" json:"code"`
  5. CreatedAt sql.NullTime `db:"createdAt" json:"createdAt"`
  6. UpdatedAt sql.NullTime `db:"updatedAt" json:"updatedAt"`
  7. }
  8. type temp struct {
  9. Currency map[string]Currency
  10. }
  11. func GetCurrencies(db *sqlx.DB) ([]Currency, error) {
  12. sqlStatement := `SELECT * FROM "Currencies"`
  13. var currencyRows []Currency
  14. err := db.Select(&currencyRows, sqlStatement)
  15. return currencyRows, err
  16. }
  17. func PrintC(db *sqlx.DB) {
  18. Currencies, _ := GetCurrencies(db)
  19. stores := []temp{}
  20. for _, currency := range Currencies {
  21. store := temp{
  22. Currency: map[string]Currency{
  23. currency.Name: {
  24. ID: currency.ID,
  25. Name: currency.Name,
  26. Code: currency.Code,
  27. CreatedAt: currency.CreatedAt,
  28. UpdatedAt: currency.UpdatedAt,
  29. },
  30. },
  31. }
  32. stores = append(stores, store)
  33. }
  34. fmt.Println(stores)
  35. }

Current Output:

  1. [{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包会查找这个接口来打印值。

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

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.

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

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:

确定