当前位置:网站首页>golang 基础 ——map、数组、切片 存放不同类型的数据
golang 基础 ——map、数组、切片 存放不同类型的数据
2022-07-05 08:41:00 【猎人在吃肉】
基础知识,不解释,直接看代码
package main
import (
"fmt"
)
type User struct {
ID string
Name string
Age int
}
func main() {
fmt.Println("------------------- map -------------------------")
data := map[string]interface{
}{
} // 注意:是2个大括号
data["num"] = 123 // int 类型
data["str"] = "helloworld" // 字符串类型
user := &User{
ID: "1001", Name: "zhangsan", Age: 18}
data["user1"] = user // User对象类型
for k, v := range data {
fmt.Printf("k= %v , v的类型是 %T ,v= %v \n", k, v, v)
}
fmt.Println("----------------- 数组 ---------------------")
var paramters []interface{
} // 定义切片,注意:有1个大括号
paramters = append(paramters, 456) // int 类型
paramters = append(paramters, "李四") // 字符串类型
paramters = append(paramters, user) // User对象类型
for k, v := range paramters {
fmt.Printf("k= %v , v的类型是 %T ,v= %v \n", k, v, v)
}
}
运行结果:
------------------- map -------------------------
k= num , v的类型是 int ,v= 123
k= str , v的类型是 string ,v= helloworld
k= user1 , v的类型是 *main.User ,v= &{
1001 zhangsan 18}
----------------- 数组 ---------------------
k= 0 , v的类型是 int ,v= 456
k= 1 , v的类型是 string ,v= 李四
k= 2 , v的类型是 *main.User ,v= &{
1001 zhangsan 18}
边栏推荐
- Business modeling of software model | object modeling
- Sword finger offer 09 Implementing queues with two stacks
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- 【日常训练】1200. 最小绝对差
- Affected tree (tree DP)
- 实例006:斐波那契数列
- 实例007:copy 将一个列表的数据复制到另一个列表中。
- 猜谜语啦(5)
- 2020-05-21
- Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
猜你喜欢
随机推荐
Classification of plastic surgery: short in long long long
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Business modeling of software model | stakeholders
Stm32--- systick timer
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
How apaas is applied in different organizational structures
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
[three tier architecture and JDBC summary]
GEO数据库中搜索数据
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Affected tree (tree DP)
Explore the authentication mechanism of StarUML
猜谜语啦(3)
第十八章 使用工作队列管理器(一)
Cinq détails de conception du régulateur de tension linéaire
MATLAB skills (28) Fuzzy Comprehensive Evaluation
How to write cover letter?
[daily training] 1200 Minimum absolute difference
Speech recognition learning summary
Old Wang's esp8266 and old Wu's ws2818 light strip


![[牛客网刷题 Day4] JZ55 二叉树的深度](/img/f7/ca8ad43b8d9bf13df949b2f00f6d6c.png)
![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)




