码迷,mamicode.com
首页 > 其他好文 > 详细

Golang中 struct{} 和 struct{}{}区别

时间:2020-04-07 20:37:30      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:initial   str   定义   lang   add   col   内容   Golan   使用   

struct是Go中的关键字,用于定义结构类型。
例如:

type User struct {
    Name string
    Age  int
}

struct {} :表示struct类型

struct {}是一个无元素的结构体类型,通常在没有信息存储时使用。优点是大小为0,不需要内存来存储struct {}类型的值。

struct {} {}:表示struct类型的值,该值也是空。

struct {} {}是一个复合字面量,它构造了一个struct {}类型的值,该值也是空。

例子

var set map[string]struct{}
// Initialize the set
set = make(map[string]struct{})

// Add some values to the set:
set["red"] = struct{}{}
set["blue"] = struct{}{}

// Check if a value is in the map:
_, ok := set["red"]
fmt.Println("Is red in the map?", ok)
_, ok = set["green"]
fmt.Println("Is green in the map?", ok)

输出内容

Is red in the map? true
Is green in the map? false

Golang中 struct{} 和 struct{}{}区别

标签:initial   str   定义   lang   add   col   内容   Golan   使用   

原文地址:https://www.cnblogs.com/show58/p/12655375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!