当前位置:网站首页>Golang foundation -- map, array and slice store different types of data
Golang foundation -- map, array and slice store different types of data
2022-07-05 08:50:00 【The hunter is eating meat】
Basic knowledge of , Don't explain , Look directly at the code
package main
import (
"fmt"
)
type User struct {
ID string
Name string
Age int
}
func main() {
fmt.Println("------------------- map -------------------------")
data := map[string]interface{
}{
} // Be careful : yes 2 Two braces
data["num"] = 123 // int type
data["str"] = "helloworld" // String type
user := &User{
ID: "1001", Name: "zhangsan", Age: 18}
data["user1"] = user // User object type
for k, v := range data {
fmt.Printf("k= %v , v The type is %T ,v= %v \n", k, v, v)
}
fmt.Println("----------------- Array ---------------------")
var paramters []interface{
} // Defining slices , Be careful : Yes 1 Two braces
paramters = append(paramters, 456) // int type
paramters = append(paramters, " Li Si ") // String type
paramters = append(paramters, user) // User object type
for k, v := range paramters {
fmt.Printf("k= %v , v The type is %T ,v= %v \n", k, v, v)
}
}
Running results :
------------------- map -------------------------
k= num , v The type is int ,v= 123
k= str , v The type is string ,v= helloworld
k= user1 , v The type is *main.User ,v= &{
1001 zhangsan 18}
----------------- Array ---------------------
k= 0 , v The type is int ,v= 456
k= 1 , v The type is string ,v= Li Si
k= 2 , v The type is *main.User ,v= &{
1001 zhangsan 18}
边栏推荐
- Count of C # LINQ source code analysis
- TF coordinate transformation of common components of ros-9 ROS
- Meta tag details
- Configuration and startup of kubedm series-02-kubelet
- kubeadm系列-00-overview
- 【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
- [牛客网刷题 Day4] JZ35 复杂链表的复制
- My university
- 深度学习模型与湿实验的结合,有望用于代谢通量分析
- Basic number theory - factors
猜你喜欢
随机推荐
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Run menu analysis
Guess riddles (142)
My university
Guess riddles (5)
Programming implementation of ROS learning 6 -service node
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
Kubedm series-00-overview
图解网络:什么是网关负载均衡协议GLBP?
微信H5公众号获取openid爬坑记
Task failed task_ 1641530057069_ 0002_ m_ 000000
Bit operation related operations
Mathematical modeling: factor analysis
Halcon snap, get the area and position of coins
How apaas is applied in different organizational structures
Business modeling of software model | vision
Business modeling | process of software model
Several problems to be considered and solved in the design of multi tenant architecture
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Use and programming method of ros-8 parameters