当前位置:网站首页>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}
边栏推荐
- Confusing basic concepts member variables local variables global variables
- Use arm neon operation to improve memory copy speed
- Oracle advanced (III) detailed explanation of data dictionary
- ORACLE进阶(三)数据字典详解
- Lori remote control LEGO motor
- File server migration scheme of a company
- Guess riddles (5)
- RT-Thread内核快速入门,内核实现与应用开发学习随笔记
- [daily training -- Tencent selected 50] 557 Reverse word III in string
- [matlab] matlab reads and writes Excel
猜你喜欢
随机推荐
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
Task failed task_ 1641530057069_ 0002_ m_ 000000
Guess riddles (9)
Infix expression evaluation
Old Wang's esp8266 and old Wu's ws2818 light strip
696. 计数二进制子串
C语言标准函数scanf不安全的原因
Warning: retrying occurs during PIP installation
[牛客网刷题 Day4] JZ55 二叉树的深度
File server migration scheme of a company
Low code platform | apaas platform construction analysis
Illustrated network: what is gateway load balancing protocol GLBP?
Guess riddles (10)
深度学习模型与湿实验的结合,有望用于代谢通量分析
ECMAScript6介绍及环境搭建
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
Guess riddles (2)
Basic number theory - factors
Solutions of ordinary differential equations (2) examples
Explore the authentication mechanism of StarUML