当前位置:网站首页>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
- Five design details of linear regulator
- An enterprise information integration system
- The first week of summer vacation
- Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
- IT冷知识(更新ing~)
- Business modeling of software model | vision
- Lori remote control commissioning record
- Run菜单解析
- [matlab] matlab reads and writes Excel
猜你喜欢
STM32 --- serial port communication
How apaas is applied in different organizational structures
猜谜语啦(4)
319. 灯泡开关
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
319. Bulb switch
猜谜语啦(6)
IT冷知识(更新ing~)
剑指 Offer 09. 用两个栈实现队列
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
随机推荐
Search data in geo database
STM32 --- configuration of external interrupt
Speech recognition learning summary
2020-05-21
暑假第一周
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Halcon blob analysis (ball.hdev)
Typescript hands-on tutorial, easy to understand
How to write cover letter?
猜谜语啦(9)
Business modeling | process of software model
C# LINQ源码分析之Count
Cinq détails de conception du régulateur de tension linéaire
One dimensional vector transpose point multiplication np dot
99 multiplication table (C language)
【日常训练--腾讯精选50】557. 反转字符串中的单词 III
【三层架构及JDBC总结】
How to manage the performance of R & D team?
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
猜谜语啦(4)