当前位置:网站首页>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}
边栏推荐
- Classification of plastic surgery: short in long long long
- 猜谜语啦(4)
- [daiy4] jz32 print binary tree from top to bottom
- Reasons for the insecurity of C language standard function scanf
- Guess riddles (10)
- C语言标准函数scanf不安全的原因
- Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
- Use arm neon operation to improve memory copy speed
- 猜谜语啦(7)
- 猜谜语啦(8)
猜你喜欢

Shift operation of complement

Wechat H5 official account to get openid climbing account

Digital analog 1: linear programming

猜谜语啦(2)

猜谜语啦(9)

Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc

Ros-10 roslaunch summary

Run menu analysis

Solutions of ordinary differential equations (2) examples

How to manage the performance of R & D team?
随机推荐
Speech recognition learning summary
ORACLE进阶(三)数据字典详解
轮子1:QCustomPlot初始化模板
Guess riddles (3)
Typescript hands-on tutorial, easy to understand
Mengxin summary of LIS (longest ascending subsequence) topics
猜谜语啦(10)
[daiy4] copy of JZ35 complex linked list
Solutions of ordinary differential equations (2) examples
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
Halcon wood texture recognition
使用arm Neon操作,提高内存拷贝速度
Explore the authentication mechanism of StarUML
Beautiful soup parsing and extracting data
Run菜单解析
猜谜语啦(2)
Wheel 1:qcustomplot initialization template
Run menu analysis
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
asp.net(c#)的货币格式化