当前位置:网站首页>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}
边栏推荐
- Solutions of ordinary differential equations (2) examples
- Several problems to be considered and solved in the design of multi tenant architecture
- [daily training] 1200 Minimum absolute difference
- 资源变现小程序添加折扣充值和折扣影票插件
- C语言标准函数scanf不安全的原因
- 特征工程
- Task failed task_ 1641530057069_ 0002_ m_ 000000
- How can fresh students write resumes to attract HR and interviewers
- Halcon clolor_ pieces. Hedv: classifier_ Color recognition
- Lori remote control commissioning record
猜你喜欢

Business modeling of software model | stakeholders

Halcon color recognition_ fuses. hdev:classify fuses by color

Typescript hands-on tutorial, easy to understand

Guess riddles (11)

It cold knowledge (updating ing~)

Illustration of eight classic pointer written test questions

Halcon blob analysis (ball.hdev)

Ros-10 roslaunch summary

Guess riddles (2)

Digital analog 1: linear programming
随机推荐
C# LINQ源码分析之Count
Guess riddles (10)
图解网络:什么是网关负载均衡协议GLBP?
猜谜语啦(9)
Run menu analysis
Guess riddles (142)
Oracle advanced (III) detailed explanation of data dictionary
Ros-10 roslaunch summary
[matlab] matlab reads and writes Excel
Configuration and startup of kubedm series-02-kubelet
Business modeling of software model | stakeholders
Wechat H5 official account to get openid climbing account
Halcon affine transformations to regions
C [essential skills] use of configurationmanager class (use of file app.config)
kubeadm系列-01-preflight究竟有多少check
我从技术到产品经理的几点体会
Guess riddles (6)
Halcon: check of blob analysis_ Blister capsule detection
Halcon blob analysis (ball.hdev)
Halcon shape_ trans