(This is an unofficial Go FAQ. The official one is here.)

What does the compile error message non-name *** on left side of := mean?

:= must be pure All items at the left side ofmust be pure identifiers and at least one of them must be a new variable name.

Up to now (Go 1.15), there is a mandatory rule for short variable declarations:

This means container elements ( x[i] ), struct fields ( x.f ), pointer dereferences ( *p ) and qualified identifiers ( aPackage.Value ) can't appear at the left side of := .

Currently, there is an open issue (which was merged with a more related one) for this problem. It looks Go core team wants to leave this problem unresolved currently.

What does the compile error message unexpected newline, expecting { ... mean?

In Go, we can't break a code line at an arbitrary position. Please read line break rules in Go for details. By the rules, generally, it is not okay to break code lines just before the open brackets. For example, the following code if true { } for i := 0; i < 10; i++ { } var _ = []int { 1, 2, 3 } will be interpreted as if true; { } for i := 0; i < 10; i++; { } var _ = []int; { 1, 2, 3; } Go compilers will report an error for each open bracket { . To avoid these errors, we should rewrite the above code as the following. if true { } for i := 0; i < 10; i++ { } var _ = []int { 1, 2, 3, } For example, the following codewill be interpreted asGo compilers will report an error for each open bracket. To avoid these errors, we should rewrite the above code as the following.

What does the compiler error message declared and not used mean?

For the standard Go compiler, each variable declared in local code blocks must be used as a r-value (right-hand-side value) for at least once. So the following code fails to compile. func f(x bool) { var y = 1 // y declared but not used (as r-values) if x { y = 2 // here y is used as a left-hand-side value } } So the following code fails to compile.

Does Go runtime maintain the iteration orders for maps?

No. Go 1 specification says the iteration order over a map is not specified and is not guaranteed to be the same from one iteration to the next. For the standard Go compiler, the map iteration orders are some randomized. If you require a stable iteration order for a map you must maintain the order by yourself. Please read Go maps in action for more information.

However, please note, since Go 1.12, the entry order in the print result of the print functions in standard packages are always ordered.

Will Go compilers do padding to ensure field alignments for struct types?

At least for the standard Go compiler and gccgo, the answer is yes. How many bytes will be padded is OS and compiler dependent. Please read memory layouts for details.

Go Compilers will not rearrange struct fields to minimize struct value sizes. Doing this may cause some unexpected results. However, programmers can minimize padding by reordering the fields manually.

Why does the final field of a zero-sized type in a struct contribute to the size of the struct sometimes?

In the current standard Go runtime implementation, if a memory block is referenced by at least one active pointer, then the memory block will not be viewed as garbage and will not be collected for sure. All the fields of an addressable struct value can be taken addresses. If the size of the final field in a non-zero-sized struct value is zero, then taking the address of the final field in the struct value will return an address which is beyond the allocated memory block for the struct value. The returned address may point to another allocated memory block which closely follows the one allocated for the non-zero-sized struct value. As long as the returned address is stored in an active pointer value, the other allocated memory block will not get garbage collected, which may cause memory leaking. To avoid the problems, the standard Go compiler will ensure that taking the address of the final field in a non-zero-sized struct will never return an address which is beyond the allocated memory block for the struct. The standard Go compiler implements this by padding some bytes after the final zero-sized field when needed. If the types of all fields in a struct type are zero-sized (so the struct is also a zero-sized type), then there is no need to pad bytes in the struct, for the standard Go compiler treats zero-sized memory blocks specially. An example: package main import ( "unsafe" "fmt" ) func main() { type T1 struct { a struct{} x int64 } fmt.Println(unsafe.Sizeof(T1{})) // 8 type T2 struct { x int64 a struct{} } fmt.Println(unsafe.Sizeof(T2{})) // 16 } An example:

Is new(T) a sugar of var t T; (&t) ?

Generally we can think so, though there would some subtle differences between the two, depending on compiler implementations. The memory block allocated by new may be either on stack or on heap.

What does the runtime error message all goroutines are asleep - deadlock mean?

The word asleep is not accurate here, it means in blocking state in fact.

As a blocking goroutine can only be unblocked by another goroutine, if all goroutines in a program enter blocking state, then all of they will stay in blocking state for ever. This means the program is deadlocked. A normal running program is never expected to be deadlocked, so the standard Go runtime makes the program crash and exit.

Are 64-bit integer values guaranteed to be 64-bit aligned so that they can be accessed atomically?

The addresses passed to the 64-bit functions in sync/atomic package must be 64-bit aligned, otherwise, calls to these functions may panic at run time.

For the standard Go compiler and gccgo compiler, on 64-bit architectures, 64-bit integers are guaranteed to be 64-bit aligned. So they can be always accessed atomically without any problems.

On 32-bit architectures, 64-bit integers are only guaranteed to be 32-bit aligned. So accessing many 64-bit integers atomically may cause panics. However, there are some ways to guarantee some 64-bit integers to be relied upon to be 64-bit aligned. Please read memory layouts in Go for details.

Are assignments of values atomic operations?

No for the standard Go compiler, even if the sizes of the assigned values are native words.

Please read the official question for more details.

Is every zero value composed of a sequence of zero bytes in memory?

For most types, this is true. In fact, this is compiler dependent. For example, for the standard Go compiler, the statement is wrong for some zero values of string types. Evidence: package main import ( "unsafe" "fmt" ) func main() { var s1 string fmt.Println(s1 == "") // true fmt.Println(*(*uintptr)(unsafe.Pointer(&s1))) // 0 var s2 = "abc"[0:0] fmt.Println(s2 == "") // true fmt.Println(*(*uintptr)(unsafe.Pointer(&s2))) // 4869856 fmt.Println(s1 == s2) // true } Evidence:

Inversely, for all the architectures the standard Go compiler currently supports, if all bytes in a value are zero, then the value must be a zero value of its type. However, Go specification doesn't guarantee this. I have heard of that on some very old processors, nil pointers are not zero in memory.

Does the standard Go compiler support function inline?

Yes, the standard Go compiler supports function inline. The compiler will inline short leaf functions, automatically. Leaf functions are the functions which doesn't contain function calls. The specific inline rules may change from version to version.

there is no explicit ways to specify which functions should be inlined in user programs.

although -gcflags "-l" build option can prevent any functions being inlined, there is no formal ways to avoid specified functions being inlined in user programs. There are two informal ways (both of them might become invalid for future standard Go compiler versions): you can add a line //go:noinline directive before a function declaration to avoid the function being inlined. as functions containing loop blocks will not be inlined, you can add a for false {} line in a function to avoid the function being inlined. (This way may become invalid later.)

build option can prevent any functions being inlined, there is no formal ways to avoid specified functions being inlined in user programs. There are two informal ways (both of them might become invalid for future standard Go compiler versions): Currently (Go Toolchain 1.15), for the standard Go compiler,

Can I use finalizers as object destructors?

In Go programs, we can set a finalizer function for an object by using the runtime.SetFinalizer function. Generally, the finalizer function will be called before the object is garbage collected. But finalizers are never intended to be used as destructors of objects. The finalizers set by runtime.SetFinalizer are not guaranteed to run. So you shouldn't rely on finalizers for your program correctness.

The main intention of finalizers is for libraries maintainers to make extra efforts to remedy the damage caused by libraries users don't use the libraries correctly. For example, in a program, if we use the os.Open to open many files but forget to close them after using them, then the program will hold many file descriptors until the program exits. This is resource leak. To avoid the program holding too many file descriptors, the maintainers of the os package will set a finalizer on the every created os.File object. The finalizer will close the file descriptor stored in the os.File object. As above mentioned, the finalizers are not guaranteed to be called. They are just used to make the extent of resource leak as small as possible.

Please note, some finalizers will never get called for sure, and sometimes setting finalizers improperly will prevent some objects from being garbage collected. Please read the runtime.SetFinalizer function documentation to get more details.

How to get the number of days of any month by using as few code lines as possible?

days := time.Date(year, month+1, 0, 0, 0, 0, 0, time.UTC).Day() For Go time APIs, the usual month range is [1, 12] and the start day of each month is 1 . The start time of a month m in year y is time.Date(y, m, 1, 0, 0, 0, 0, time.UTC) . Assume the input year is a nature year and the input month is also a nature month (January is 1).For Go time APIs, the usual month range isand the start day of each month is. The start time of a monthin yearis The arguments passed to time.Date can be outside their usual ranges and will be normalized during the conversion. For example, January 32 will be converted to February 1. Here are some time.Date use examples in Go: package main import ( "time" "fmt" ) func main() { // 2017-02-01 00:00:00 +0000 UTC fmt.Println(time.Date(2017, 1, 32, 0, 0, 0, 0, time.UTC)) // 2017-01-31 23:59:59.999999999 +0000 UTC fmt.Println(time.Date(2017, 1, 32, 0, 0, 0, -1, time.UTC)) // 2017-01-31 00:00:00 +0000 UTC fmt.Println(time.Date(2017, 2, 0, 0, 0, 0, 0, time.UTC)) // 2016-12-31 00:00:00 +0000 UTC fmt.Println(time.Date(2016, 13, 0, 0, 0, 0, 0, time.UTC)) // 2017-02-01 00:00:00 +0000 UTC fmt.Println(time.Date(2016, 13, 32, 0, 0, 0, 0, time.UTC)) } Here are someuse examples in Go:

What is the difference between the function call time.Sleep(d) and the channel receive operation <-time.After(d) ?

The two will both pause the current goroutine execution for a certain duration. The difference is the function call time.Sleep(d) will let the current goroutine enter sleeping sub-state, but still stay in running state, whereas, the channel receive operation <-time.After(d) will let the current goroutine enter blocking state.

Calls of the TrimLeft and TrimRight functions in the strings and bytes standard packages often return unexpected results, are there bugs in these function implementations?

Aha, maybe there are bugs in the implementations, but none are confirmed now. If the return results are unexpected, it is more possible that your expectations are not correct.

strings and bytes standard packages. These functions can be categorized into two groups: Trim , TrimLeft , TrimRight , TrimSpace , TrimFunc , TrimLeftFunc , TrimRightFunc . These functions will trim all leading or trailing UTF-8-encoded Unicode code points (a.k.a. runes) which satisfy the specified or implied conditions ( TrimSpace implies to trim all kinds of white spaces). Each of the leading or trailing runes will be checked until one doesn't satisfy the specified or implied conditions. TrimPrefix , TrimSuffix . The two functions will trim the specified prefix or suffix substrings (or subslices) as a whole. There are many trim functions inandstandard packages. These functions can be categorized into two groups: Some programmers misused the TrimLeft and TrimRight functions as TrimPrefix and TrimSuffix functions when they use the trim functions the first time. Certainly, the return results are very possible not as expected. Example: package main import ( "fmt" "strings" ) func main() { var s = "abaay森z众xbbab" o := fmt.Println o(strings.TrimPrefix(s, "ab")) // aay森z众xbbab o(strings.TrimSuffix(s, "ab")) // abaay森z众xbb o(strings.TrimLeft(s, "ab")) // y森z众xbbab o(strings.TrimRight(s, "ab")) // abaay森z众x o(strings.Trim(s, "ab")) // y森z众x o(strings.TrimFunc(s, func(r rune) bool { return r < 128 // trim all ascii chars })) // 森z众 } Example:

What are the differences between the fmt.Print and fmt.Println functions?

The fmt.Println function will always write a space between two adjacent arguments, whereas the fmt.Print function will write a space between two adjacent arguments only if both of (the concrete values of) the two adjacent arguments are not strings.

Another difference is fmt.Println will write a newline character in the end, but the fmt.Print function will not.

Is there any difference between the log.Print and log.Println functions?

The difference between the log.Print and log.Println functions is the sams as the first difference between the fmt.Print and fmt.Println functions described in the last question.

Both of the two functions will write a newline character in the end.

Are fmt.Print , fmt.Println and fmt.Printf functions synchronized?

No, these functions are not synchronized. Please use the corresponding functions in the log standard package instead when synchronizations are needed. You can call log.SetFlags(0) to remove the prefix from each log line.

What are the differences between the built-in print / println functions and the corresponding print functions in the fmt and log standard packages?

The built-in print / println functions will write to the standard error. The print functions in the fmt standard package will write to the standard output. The print functions in the log standard package will write to the standard error by default, whereas which can be configured through the log.SetOutput function. Calls to the built-in print / println functions can't take array and struct arguments. For an argument of a composite type, the built-in print / println functions write the addresses of the underlying value parts of the argument, whereas the print functions in the fmt and log standard packages try to write the value literal of the dynamic values of the interface arguments. Currently (Go Toolchain 1.15), for the standard Go compiler, calls to the built-in print / println functions will not make the values referenced by the arguments of the calls escape to heap, whereas the print functions in the fmt and log standard packages will. If an argument has a String() string or Error() string method, the print functions in the fmt and log standard packages will try to call that method when writing the argument, whereas the built-in print / println functions will ignore methods of arguments. The built-in print / println functions are not guaranteed to exist in future Go versions. Besides the difference mentioned in the last question, there are some other differences between the three sets of functions.

What is the difference between the random numbers produced by the math/rand standard package and the crypto/rand standard package?

The pseudo random numbers produced by the math/rand standard package are deterministic for a given seed. The produced random numbers are not good for security-sensitive contexts. For cryptographical security purpose, we should use the pseudo random numbers produced by the crypto/rand standard package.

Why isn't there a math.Round function?

There is a math.Round function, but only since Go 1.10. Two new functions, math.Round and math.RoundToEven have been added since Go 1.10.

Before Go 1.10, there is a long time in disputing whether or not the math.Round function should be added to standard package or not. In the end, the proposal is adopted.

Which types don't support comparisons?

map

slice

function

struct types containing incomparable fields

array types with incomparable element types Following types don't support comparisons: Types which don't support comparisons can't be used as the key type of map types.

although map, slice and function types don't support comparisons, their values can be compared to the bare nil identifier.

identifier. comparing two interface values with will panic at run time if the two dynamic types of the two interface values are identical and incomparable. Please note, On why slice, map and function types don't support comparison, please read this answer in the official Go FAQ.

Why aren't two nil values equal sometimes?

(The answer in the official Go FAQ may also answer this question.)

An interface value can be viewed as a box which is used to encapsulate non-interface values. Only values whose types implement the type of the interface value can be boxed (encapsulated) into the interface value. In Go, there are several kinds of types whose zero values are represented as the predeclared identifier nil . An interface value boxing nothing is a zero interface value, a.k.a, a nil interface value. However an interface value boxing a nil non-interface value doesn't box nothing, so it is not, and doesn't equal to, a nil interface value. When comparing a nil interface value and a nil non-interface value (assume they can be compared), the nil non-interface value will be converted to the type of the nil interface value before doing the comparison. The conversion result is an interface value boxing a copy of the non-interface value. The result interface value doesn't box nothing, so it is not, or doesn't equal to, the nil interface value. Please read interfaces in Go and nils in Go for detailed explanations. For example: package main import "fmt" func main() { var pi *int = nil var pb *bool = nil var x interface{} = pi var y interface{} = pb var z interface{} = nil fmt.Println(x == y) // false fmt.Println(x == nil) // false fmt.Println(y == nil) // false fmt.Println(x == z) // false fmt.Println(y == z) // false } For example:

Why don't type []T1 and []T2 share the same underlying type even if the two different types T1 and T2 share the same underlying type?

(It looks the official Go FAQ also added a similar question not long ago.)

In Go, values of a slice type can be converted to another slice type without using the unsafe mechanisms only if the two slice types share the same underlying type. (This article lists the full list of value conversion rules.) The underlying type of a non-defined composite type is the composite type itself. So even if two different types T1 and T2 share the same underlying type, type []T1 and []T2 are still different types, so their underlying types are also different, which means values of one of them can't be converted to the other. The reasons for the underlying types of []T1 and []T2 are not same are: the request of converting values of []T1 and []T2 to each other is not strong in practice.

and to each other is not strong in practice. to make underlying type tracing rule simpler. The reasons for the underlying types ofandare not same are: The same reasons are also valid for other composite types. For example, type map[T]T1 and map[T]T2 also don't share the same underlying type even if T1 and T2 share the same underlying type. It is possible that values of type []T1 can be converted to []T2 by using the unsafe mechanisms, but generally this is not recommended: package main import ( "fmt" "unsafe" ) func main() { type MyInt int var a = []int{7, 8, 9} var b = *(*[]MyInt)(unsafe.Pointer(&a)) b[0]= 123 fmt.Println(a) // [123 8 9] fmt.Println(b) // [123 8 9] fmt.Printf("%T

", a) // []int fmt.Printf("%T

", b) // []main.MyInt } It is possible that values of typecan be converted toby using themechanisms, but generally this is not recommended:

Which values can and which values can't be taken addresses?

bytes in strings

map elements

dynamic values of interface values (exposed by type assertions)

constant values (including named constants and literals)

package level functions

methods (used as function values)

intermediate values function calls explicit value conversions all sorts of operations, excluding pointer dereference operations, but including: channel receive operations sub-string operations sub-slice operations addition, subtraction, multiplication, and division, etc.

Following values can't be taken addresses:

Please note, there is a syntax sugar, &T{} , in Go. It is a short form of tmp := T{}; (&tmp) . So &T{} is legal doesn't mean the literal T{} is addressable.

variables

fields of addressable structs

elements of addressable arrays

elements of any slices (whether the slices are addressable or not)

pointer dereference operations Following values can be taken addresses:

Why are map elements unaddressable?

In Go, a map is designed as a container which can contain unlimited number of entries if memory is available. And, in the official Go runtime implementation, to ensure good map element indexing efficiency, each map value only maintains one continuous memory segment for the entire entries stored in that map. Therefor, Go runtime needs to allocate larger memory segments for a map from time to time when there are more and more entries being put into the map. In the process, the entries stored on older memory segments will be moved to newer memory segments. There might be also some other reasons causing entry memory movements. In other words, the addresses of map elements will change from time to time on need. If map elements are allowed to be taken addresses, then when some map entries are moved, Go runtime must update all pointers which are storing the addresses of the moved elements, which brings many difficulties in implemnting Go compilers and runtimes and decreases Go program running performance much. So, currently, map elements are disallowed to be taken addresses.

Another reason why map elements are unaddressable is the map index expression aMap[key] might return an element stored in map aMap or not, which means aMap[key] might still result a zero value after (&aMap[key]).Modify() is called. This will confuse many people.

Why elements of a non-nil slice are always addressable, even if the slice is unaddressable?

struct { // elements references an element sequence. elements unsafe.Pointer length int capacity int } The internal type for slices is a struct like Each slice indirectly references an underlying element sequence internally. Although a non-nil slice is not addressable, its internal element sequence is always allocated somewhere and must be addressable. Taking addresses of elements of a slice is taking the addresses of elements of the internal element sequence actually. This is why elements of unaddressable non-nil slices are always addressable.

For any non-pointer non-interface defined type T , why is the method set of *T always a super set of the method set of T , but not vice versa?

a value of type T can call methods defined on type *T , but only if the value of T are addressable. Compilers will take the address of the T value automatically before calling the pointer receiver methods. For not any values of type T are addressable, not any values of type T are capable of calling methods defined on type *T . This convenience is just a sugar, not an intrinsic rule.

can call methods defined on type , but only if the value of are addressable. Compilers will take the address of the value automatically before calling the pointer receiver methods. For not any values of type are addressable, not any values of type are capable of calling methods defined on type . This convenience is just a sugar, not an intrinsic rule. a value of type *T can always call methods defined on type T . This is because it is always legal to dereference a pointer value. This convenience is not only a sugar, but also an intrinsic rule. In Go, for convenience, So it is much reasonable that the method set of *T is always a super set of the method set of T , but not vice versa.

T , an implicit method with the same name and the same signature is automatically declared on type *T . Please read func (t T) MethodX(v0 ParamType0, ...) (ResultType0, ...) { ... } // An implicit method of *T is automatically defined as func (pt *T) MethodX(v0 ParamType0, ...) (ResultType0, ...) { return (*pt).MethodX(v0, ...) } In fact, you can think that, for every method declared on type, an implicit method with the same name and the same signature is automatically declared on type. Please read methods for details.

Please read this answer in the official Go FAQ to get more explanations.

Which types can we implement methods for?

Please read methods in Go for details.

How to declare immutable values in Go?

immutable value definitions: the values which have no addresses (so they are not addressable). the values which have addresses but are not addressable (their addresses are not allowed to be taken in syntax). the values which are addressable but their values are not allowed to be modified in syntax. There are threedefinitions: In Go, up to now (Go 1.15), there are no values satisfy the third definition. In other words, the third definition is not supported. Name constant values satisfy the first definition. Methods and package-level functions can also viewed as declared immutable values. They satisfy the second definition. String elements (bytes) also satisfy the second definition. There are no ways to declare other custom immutable named values in Go.

Why isn't there a built-in set container type?

Sets are just maps but don't care about element values. In Go, map[Tkey]struct{} is often used as a set type.

What is byte? What is rune? How to convert []byte and []rune values to strings?

In Go, byte is an alias of type uint8 . In other words, byte and uint8 are the same identical type. The same relation is for rune and int32 .

A rune often is used to store a Unicode code point.

[]byte and []rune values can be explicitly and directly converted to strings, and vice versa. package main import "fmt" func main() { var s0 = "Go" var bs = []byte(s0) var s1 = string(bs) var rs = []rune(s0) var s2 = string(rs) fmt.Println(s0 == s1) // true fmt.Println(s0 == s2) // true } andvalues can be explicitly and directly converted to strings, and vice versa. About more on strings, please read strings in Go.

How to manipulate pointer values atomically?

import ( "unsafe" "sync/atomic" ) type T int // just a demo var p *T func demo(newP *T) { // load var _ = (*T)(atomic.LoadPointer( (*unsafe.Pointer)(unsafe.Pointer(&p)), )) // store atomic.StorePointer( (*unsafe.Pointer)(unsafe.Pointer(&p)), unsafe.Pointer(newP), ) // swap var oldP = (*T)(atomic.SwapPointer( (*unsafe.Pointer)(unsafe.Pointer(&p)), unsafe.Pointer(newP), )) // compare and swap var swapped = atomic.CompareAndSwapPointer( (*unsafe.Pointer)(unsafe.Pointer(&p)), unsafe.Pointer(oldP), unsafe.Pointer(newP), ) _ = swapped } Example: Yes, now it is much verbose to use the pointer atomic functions.

What does iota mean?

Iota is the ninth letter of the Greek alphabet. In Go, iota is used in constant declarations. In each constant declaration group, its value is N in the Nth constant specification in that constant declaration group.

Why isn't there a built-in closed function to check whether or not a channel is closed?

The reason is the usefulness of such function is very limited. The return result of a call to such function may be not able to reflect the latest status of the input channel argument. So it is not a good idea to make decisions relying on the return result.

If you do need such a function, it would be effortless to write one by yourself. Please read this article to get how to write closed functions and how to avoid using such a function.

Is it safe for a function to return pointers of local variables?

Yes, it is absolutely safe in Go.

Go compilers which support stack will do escape analysis. For the standard Go compiler, if the escape analyzer thinks a memory block will only be used in current function call for sure, it will allocate the memory block on stack, otherwise, then the memory block will be allocated on heap. Please read memory block for more information.

What does the word gopher mean in Go community?

In Go community, a gopher means a Go programmer. This nickname may be originated from the fact that Go language adopted a cartoon gopher as the mascot. BTW, the cartoon gopher is designed by Renee French, who is the wife of the (first) Go project leader, Rob Pike.