当前位置:网站首页>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}
边栏推荐
- Guess riddles (3)
- Sword finger offer 09 Implementing queues with two stacks
- STM32 --- serial port communication
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
- STM32---IIC
- Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
- How apaas is applied in different organizational structures
- [formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
猜你喜欢
How apaas is applied in different organizational structures
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Arduino operation stm32
猜谜语啦(10)
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
实例009:暂停一秒输出
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
图解八道经典指针笔试题
Guess riddles (10)
随机推荐
Example 008: 99 multiplication table
Arduino burning program and Arduino burning bootloader
实例010:给人看的时间
Basic number theory - fast power
Several problems to be considered and solved in the design of multi tenant architecture
Is the security account given by Yixue school safe? Where can I open an account
EA introduction notes
STM32---ADC
2020-05-21
【NOI模拟赛】汁树(树形DP)
GEO数据库中搜索数据
[牛客网刷题 Day4] JZ55 二叉树的深度
Redis实现高性能的全文搜索引擎---RediSearch
How can fresh students write resumes to attract HR and interviewers
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
99 multiplication table (C language)
实例008:九九乘法表
猜谜语啦(142)
關於線性穩壓器的五個設計細節
MATLAB小技巧(28)模糊綜合評價