当前位置:网站首页>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}
边栏推荐
- [daiy4] copy of JZ35 complex linked list
- Programming implementation of ROS learning 6 -service node
- Bit operation related operations
- Several problems to be considered and solved in the design of multi tenant architecture
- Low code platform | apaas platform construction analysis
- Business modeling | process of software model
- Guess riddles (6)
- Warning: retrying occurs during PIP installation
- The location search property gets the login user name
- ECMAScript6介绍及环境搭建
猜你喜欢
随机推荐
Business modeling of software model | stakeholders
OpenFeign
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
IT冷知识(更新ing~)
Solutions of ordinary differential equations (2) examples
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Halcon wood texture recognition
容易混淆的基本概念 成员变量 局部变量 全局变量
Halcon snap, get the area and position of coins
猜谜语啦(4)
Search data in geo database
Business modeling | process of software model
Shift operation of complement
Halcon Chinese character recognition
JS asynchronous error handling
Infix expression evaluation
[Niuke brush questions day4] jz55 depth of binary tree
Some pitfalls of win10 network sharing
Programming implementation of ROS learning 5-client node
![[牛客网刷题 Day4] JZ35 复杂链表的复制](/img/bc/ce90bb3cb6f52605255f1d6d6894b0.png)






