
php小编苹果在这里为大家介绍一下Go语言中的struct变量。在Go语言中,struct是一种自定义的数据类型,用于封装一组相关的数据字段。它类似于其他编程语言中的类或结构体,可以包含各种类型的字段,如整数、字符串、布尔值等。通过定义struct变量,我们可以方便地组织和管理数据,使代码更加清晰和易于维护。无论是在Web开发、系统编程还是其他领域,struct都是Go语言中非常重要的一个概念,值得我们深入学习和理解。
我怎样才能得到这个输出?
type datas struct {
age int
height int
weight int
}
func main(){
var age := 5
var height := 10
var weight := 15
namelist := []string{"b", "c", "d"}
for count, a := range namelist {
a := datas{age+count,height+count,weight+count}
//Expected Output: b{6,11,16} , c{7,12,17}, d{8,13,18}
}
}我找不到有关此案例的任何信息,我猜不包括此功能。对于这种情况有什么解决办法吗?
相反,您可以使用键名称将数据放在地图上
type datas struct {
age int
height int
weight int
}
func main(){
structMap := make(map[string]interface{})
age := 5
height := 10
weight := 15
namelist := []string{"b", "c", "d"}
for count, val := range namelist {
count = count + 1 // this is due to your expected output
out := datas{age+count,height+count,weight+count}
structMap[val] = out
}
fmt.Println(structMap)
// output: map[b:{6 11 16} c:{7 12 17} d:{8 13 18}]
}以上就是go 中名为 struct 的变量的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号