Value Parts

The articles following the current one will introduce more kinds of Go types. To easily and deeply understand those articles, it is best to read the following content in the current article firstly before reading those articles.

Two Categories Of Go Types

Go can be viewed as a C-family language, which can be confirmed from the two previous articles pointers in Go and structs in Go. Struct types and pointer types in Go and C are much alike.

On the other hand, Go can be also viewed as a C language framework. This is mainly reflected from the fact that Go supports several kinds of types whose value memory structures are not totally transparent, whereas the main characteristic of C types is the memory structures of C values are transparent. Each C value in memory occupies one continuous memory segment. (Such a continuous memory segment will be called a value part later.) However, values of some kinds of types may be often composed of one direct part and one or several underlying indirect parts which are referenced by the direct value part.

The following picture depicts a multi-part value.

The above paragraphs describe two categories of Go types. Types in the two categories are listed in the following two paragraphs.

The kinds of types in the first category are all C alike. These types include boolean types, numeric types, struct types, pointer types, and the later to be explained array types and unsafe pointer types. Array types will be explained in the next article and unsafe pointer types will be explained in the article type-unsafe pointers later. Each C alike value is only composed of one direct transparent value part.

Other kinds of types belong to the second category. Values of types in the category may contain underlying parts, and their direct parts are not transparent. Specifically speaking, these kinds of types include

The kinds of types in the second category bring much convenience to Go programming by encapsulating many implementation details. Different Go compilers may adopt different internal implementations for these types, but the external behaviors of values of these types must satisfy the requirements specified in Go specification.

These types will be explained detailedly in the following articles.


The types in the second category are not very fundamental types for a language, we can implement them from scratch by using the types in the first category. However, by encapsulating some common or unique functionalities and supporting these types as the first-level citizens in Go, the experiences of Go programming become enjoyable and productive.

On the other hand, these encapsulations of implementations of the types in the second category hide many internal definitions of these types. This prevents Go programmers from viewing the whole pictures of these types, and sometimes makes some obstacles to understanding Go better.

To help gophers better understand the types in the second category and their values, the following content of this article will introduce the internal structure definitions of these kinds of types. The detailed implementations of these types will not be explained here. The explanations in this article are based on, but not exactly the same as, the implementations used by the standard Go compiler.

Two Kinds Of Pointer Types In Go

Before showing the internal structure definitions of the kinds of types in the second category, let's clarify more on pointers and references.

We have learned Go pointers in the last last article. The pointer types in that article are type-safe pointer types. In fact, Go also supports type-unsafe pointer types. The unsafe.Pointer type provided in the unsafe standard package is like void* in C language. Types whose underlying types are unsafe.Pointer are called unsafe pointer types in Go 101.

In most other articles in Go 101, if not special specified, when a pointer type is mentioned, it means a type-safe pointer type. However, in the following parts of the current article, when a pointer type is mentioned, it may be either a type-safe pointer type or a type-unsafe pointer type.

A pointer value stores a memory address of another value, unless the pointer value is a nil pointer. We can say the pointer value references the other value, or the other value is referenced by the pointer value. Values can also be referenced indirectly.

A struct type with fields of pointer types can be called a pointer wrapper type. A struct type with fields of pointer wrapper type call also be called a pointer wrapper type.

(Possible) Internal Definitions Of The Types In The Second Category

The possible internal definitions of the types in the second category are shown below.

Internal Definitions Of Map, Channel And Function Types

The internal definitions of map, channel and function types are similar:
// map types
type _map *hashtableImpl // currently, for the standard Go compiler,
                         // Go maps are hashtables actually.

// channel types
type _channel *channelImpl

// function types
type _function *functionImpl

So, internally, types of the three kinds are just pointer types. In other words, the direct parts of values of these types are pointers internally. For each non-zero value of these types, its direct part (a pointer) references its indirect underlying implementation part.

Internal Definition Of Slice Types

The internal definition of slice types is like:
type _slice struct {
	elements unsafe.Pointer // referencing underlying elements
	len      int            // number of elements
	cap      int            // capacity

So, internally, slice types are pointer wrapper struct types. Each non-zero slice value has an indirect underlying part which stores the element values of the slice value. The elements field of the direct part references the indirect underlying part of the slice value.

Internal Definition Of String Types

Below is the internal definition for string types:
type _string struct {
	elements *byte // referencing underlying bytes
	len      int   // number of bytes

So string types are also pointer wrapper struct types internally. Each string value has an indirect underlying part storing the bytes of the string value, the indirect part is referenced by the elements field of that string value.

Internal Definition Of Interface Types

Below is the internal definition for general interface types:
type _interface struct {
	dynamicType  *_type         // the dynamic type
	dynamicValue unsafe.Pointer // the dynamic value

Internally, interface types are also pointer wrapper struct types. The internal definition of an interface type has two pointer fields. Each non-zero interface value has two indirect underlying parts which store the dynamic type and dynamic value of that interface value. The two indirect parts are referenced by the dynamicType and dynamicValue fields of that interface value.

In fact, for the standard Go compiler, the above definition is only used for blank interface types. Blank interface types are the interface types which don't specify any methods. We can learn more about interfaces in the article interfaces in Go later. For non-blank interface types, a definition like the following one is used.
// non-blank interface types
type _interface struct {
	dynamicTypeInfo *struct {
		dynamicType *_type       // the dynamic type
		methods     []*_function // implemented methods
	dynamicValue unsafe.Pointer // the dynamic value

The methods field of the dynamicTypeInfo field of an interface value stores the implemented methods of the dynamic type of the interface value for the (interface) type of the interface value.

Underlying Value Parts Are Not Copied In Value Assignments

Now we have learned that the internal definitions of the types in the second category are either pointer types or struct types. Certainly, Go compilers will never view values of the types in the second category as pointers or structs in user programs. These definitions are just used internally in implementing Go runtimes.

In Go, each value assignment (including parameter passing, etc) is a shallow value copy if the involved destination and source values have the same type (if their types are different, we can think that the source value will be implicitly converted to the destination type before doing that assignment). In other words, only the direct part of the source value is copied to the destination value in an value assignment. If the source value has underlying value part(s), then the direct parts of the destination and source values will reference the same underlying value part(s), in other words, the destination and source values will share the same underlying value part(s).

In fact, the above descriptions are not 100% correct in theory, for strings and interfaces. The official Go FAQ says the underlying dynamic value part of an interface value should be copied as well when the interface value is copied. However, as the dynamic value of an interface value is read only, the standard Go compiler/runtime doesn't copy the underlying dynamic value parts in copying interface values. This can be viewed as a compiler optimization. The same situation is for string values and the same optimization (made by the standard Go compiler/runtime) is made for copying string values. So, for the standard Go compiler/runtime, the descriptions in the last section are 100% correct, for values of any type.

Since an indirect underlying part may not belong to any value exclusively, it doesn't contribute to the size returned by the unsafe.Sizeof function.

About The "Reference Type" And "Reference Value" Terminologies

The word reference in Go world is a big mess. It brings many confusions to Go community. Some articles, including some official ones, use reference as qualifiers of types and values, or treat reference as the opposite of value. This is strongly not recommended in Go 101. I really don't want to dispute on this point. Here I just list some absolutely misuses of reference:

I don't mean the reference type or reference value terminologies are totally useless for Go, I just think they are not very essential, and they bring many confusions in using Go. If we do need these terminologies, I prefer to define them as pointers and pointer wrappers. And, my personal opinion is it is best to limit the reference word to only representing relations between values by using it as a verb or a noun, and never use it as an adjective. This will avoid many confusions in leaning, teaching and using Go.

The Go 101 project is hosted on both Github and Gitlab. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212