Go Data Structures: How To Find It Out?

As a Go learner, I feel it hard to remember all rules about the language in Go Tutorial or Effective Go. Fortunately, I found Russ Cox’s series of posts: Go Data Structures and Go Data Structures: Interfaces, which show data structures of basic types, structs, strings, slices and interfaces.

What is not covered by Russ’s posts is the data structure of map. Before complementing this part, in this post, I show where to find out details about Go data structures.

This post, Go Data Structures, shows that a string value in Go consisting of a pointer to the string literal and an integer of the length. It also shows that a slice value consists of a pointer to the slice data, an integer of the length and another integer of the capacity. Such information can be found in go/src/pkg/runtime/runtime.h:

struct String
{
	byte*	str;
	int32	len;
};
struct	Slice
{				// must not move anything
	byte*	array;		// actual data
	uint32	len;		// number of elements
	uint32	cap;		// allocated number of elements
};

A glance at the 4-line code snippet above is more than enough to understand the rules about slices in Effective Go:

Slices are reference types, which means that if you assign one slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array.

In the same source file, you can find other data structures, including Iface and Eface for interface, and Complex64 and Complex128 for complex values:

struct Iface
{
	Itab*	tab;
	void*	data;
};
struct Eface
{
	Type*	type;
	void*	data;
};
struct Complex64
{
	float32	real;
	float32	imag;
};
struct Complex128
{
	float64	real;
	float64	imag;
};

However, it does not seems that all data structures are defined in this single file. Exceptions include map and channel. We will talk about them in next posts.