Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do struct{} and struct{}{} work in Go?

Tags:

syntax

struct

go

I am wondering what does "struct{}" and "struct{}{}" mean in Go? An example is as follows:

array[index] = struct{}{}

or

make(map[type]struct{})
like image 663
Qi Zhang Avatar asked Jul 15 '17 21:07

Qi Zhang


People also ask

What is _ struct {} in Golang?

A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct.

How do I create a struct inside a struct in Golang?

A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure.

How do you declare a struct in Go?

Defining a struct typeThe type keyword introduces a new type. It is followed by the name of the type ( Person ) and the keyword struct to indicate that we're defining a struct . The struct contains a list of fields inside the curly braces. Each field has a name and a type.


1 Answers

struct is a keyword in Go. It is used to define struct types, which is a sequence of named elements.

For example:

type Person struct {
    Name string
    Age  int
}

The struct{} is a struct type with zero elements. It is often used when no information is to be stored. It has the benefit of being 0-sized, so usually no memory is required to store a value of type struct{}.

struct{}{} on the other hand is a composite literal, it constructs a value of type struct{}. A composite literal constructs values for types such as structs, arrays, maps and slices. Its syntax is the type followed by the elements in braces. Since the "empty" struct (struct{}) has no fields, the elements list is also empty:

 struct{}  {}
|  ^     | ^
  type     empty element list

As an example let's create a "set" in Go. Go does not have a builtin set data structure, but it has a builtin map. We can use a map as a set, as a map can only have at most one entry with a given key. And since we want to only store keys (elements) in the map, we may choose the map value type to be struct{}.

A map with string elements:

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)

Output (try it on the Go Playground):

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

Note that however it may be more convenient to use bool as the value type when creating a set out of a map, as the syntax to check if an element is in it is simpler. For details, see How can I create an array that contains unique strings?.

like image 180
icza Avatar answered Sep 28 '22 11:09

icza