英文:
How do I use struct as an alternative to maps?
问题
我读了Caleb Doxsey的《GO编程入门》。在第6章中,他有以下示例:
package main
import "fmt"
func main() {
elements := map[string]map[string]string{
"H": map[string]string{
"name": "氢",
"state": "气体",
},
"He": map[string]string{
"name": "氦",
"state": "气体",
},
"Li": map[string]string{
"name": "锂",
"state": "固体",
},
"Be": map[string]string{
"name": "铍",
"state": "固体",
},
"B": map[string]string{
"name": "硼",
"state": "固体",
},
"C": map[string]string{
"name": "碳",
"state": "固体",
},
"N": map[string]string{
"name": "氮",
"state": "气体",
},
"O": map[string]string{
"name": "氧",
"state": "气体",
},
"F": map[string]string{
"name": "氟",
"state": "气体",
},
"Ne": map[string]string{
"name": "氖",
"state": "气体",
},
}
if el, ok := elements["Li"]; ok {
fmt.Println(el["name"], el["state"])
}
}
他在注释中提到:“尽管通常会这样使用映射,但在第9章中,我们将看到一种更好的存储结构化信息的方法。”第9章是关于结构体和接口的。
对于上面的示例,使用结构体和接口来存储结构化信息会是什么样子呢?
提前感谢您的回答。
英文:
I read "An introduction to programming in GO" by Caleb Doxsey. In chapter 6 he had the following example:
package main
import "fmt"
func main() {
elements := map[string]map[string]string{
"H": map[string]string{
"name": "Hydrogen",
"state": "gas",
},
"He": map[string]string{
"name": "Helium",
"state": "gas",
},
"Li": map[string]string{
"name": "Lithium",
"state": "solid",
},
"Be": map[string]string{
"name": "Beryllium",
"state": "solid",
},
"B": map[string]string{
"name": "Boron",
"state": "solid",
},
"C": map[string]string{
"name": "Carbon",
"state": "solid",
},
"N": map[string]string{
"name": "Nitrogen",
"state": "gas",
},
"O": map[string]string{
"name": "Oxygen",
"state": "gas",
},
"F": map[string]string{
"name": "Fluorine",
"state": "gas",
},
"Ne": map[string]string{
"name": "Neon",
"state": "gas",
},
}
if el, ok := elements["Li"]; ok {
fmt.Println(el["name"], el["state"])
}
}
He commented the example that "although maps are often used like this, in chapter 9 we will see a better way to store structured information." Chapter 9 is about Structs and Interfaces.
What does a better way to store structured information with structs and interfaces look like for the example above?
Thank you in advance.
答案1
得分: 16
type element struct {
name string
state string
}
func main() {
elements := map[string]element{
"H": {"氢", "气体"},
"He": {"氦", "气体"},
"Li": {"锂", "固体"},
"Be": {"铍", "固体"},
"B": {"硼", "固体"},
"C": {"碳", "固体"},
"N": {"氮", "气体"},
"O": {"氧", "气体"},
"F": {"氟", "气体"},
"Ne": {"氖", "气体"},
}
if el, ok := elements["Li"]; ok {
fmt.Println(el.name, el.state)
}
}
英文:
type element struct {
name string
state string
}
func main() {
elements := map[string]element{
"H": {"Hydrogen", "gas"},
"He": {"Helium", "gas"},
"Li": {"Lithium", "solid"},
"Be": {"Beryllium", "solid"},
"B": {"Boron", "solid"},
"C": {"Carbon", "solid"},
"N": {"Nitrogen", "gas"},
"O": {"Oxygen", "gas"},
"F": {"Fluorine", "gas"},
"Ne": {"Neon", "gas"},
}
if el, ok := elements["Li"]; ok {
fmt.Println(el.name, el.state)
}
}
答案2
得分: 4
例如,
package main
import "fmt"
type Element struct {
Symbol, Name, State string
}
func main() {
elements := []Element{
{"H", "氢", "气体"},
{"He", "氦", "气体"},
{"Li", "锂", "固体"},
}
symbols := make(map[string]*Element)
for i := range elements {
symbols[elements[i].Symbol] = &elements[i]
}
if el, ok := symbols["Li"]; ok {
fmt.Println(el.Symbol, el.Name, el.State)
}
}
输出:
Li 锂 固体
英文:
For example,
package main
import "fmt"
type Element struct {
Symbol, Name, State string
}
func main() {
elements := []Element{
{"H", "Hydrogen", "gas"},
{"He", "Helium", "gas"},
{"Li", "Lithium", "solid"},
}
symbols := make(map[string]*Element)
for i := range elements {
symbols[elements[i].Symbol] = &elements[i]
}
if el, ok := symbols["Li"]; ok {
fmt.Println(el.Symbol, el.Name, el.State)
}
}
Output:
Li Lithium solid
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论