Questions tagged [go]

Go is an open-source programming language, with a syntax loosely derived from C. It's statically typed, with limited dynamic typing capabilities; it also features automatic memory management, built-in concurrency primitives, variable-length arrays – called slices –, and a large standard library.

Go (sometimes "Golang" for search-ability) is a general-purpose programming language. While originally created by Google, Go is an open source project with a large contributor base. It aims to be efficient both for development and execution with a focus on fast compilation and increased maintainability of large projects. Go was originally targeted at systems programming tasks such as building server/web applications, high throughput middleware, and databases, but it has a growing ecosystem of libraries allowing it to be used for a wide variety of tasks such as developing end-user daemons, CLIs, and desktop/mobile applications.

The first class concurrency mechanisms of Go make it easier to write programs that get the most out of multicore and networked machines, while its structural type system enables flexible and modular program construction. Go compiles quickly to memory safe machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that develops like a dynamically typed, interpreted language, but performs like native code.

Go Reference Documentation

Go Tutorials

Go Books (Paid)

Go Books (Free)

Popular Go Projects

Go Mailing Lists

Go chat

Online Go Compilers

Go FAQ

Go Code Editors & IDEs

Go Dependency Management

Go2 Resources

Go2 is an umbrella term used to indicate language changes under discussion that may break Go's compatibility promise. Even if a feature ends up being suited for a Go 1.x release, it is still often tagged and referred to as Go2. This includes proposals, draft specifications, dev implementations, etc.

70688 questions
251
votes
11 answers

How does Go compile so quickly?

I've Googled and poked around the Go website, but I can't find an explanation for Go's extraordinary build times. Are they products of the language features (or lack thereof), a highly optimized compiler, or something else? I'm not trying to promote…
Evan Kroske
  • 4,506
  • 12
  • 40
  • 59
250
votes
8 answers

Parsing RFC-3339 / ISO-8601 date-time string in Go

I tried parsing the date string "2014-09-12T11:45:26.371Z" in Go. This time format is defined as: RFC-3339 date-time ISO-8601 date-time Code layout := "2014-09-12T11:45:26.371Z" str := "2014-11-12T11:45:26.371Z" t, err := time.Parse(layout ,…
kannanrbk
  • 6,964
  • 13
  • 53
  • 94
247
votes
7 answers

What is the shortest way to simply sort an array of structs by (arbitrary) field names?

I just had a problem where I had an array of structs, e.g. package main import "log" type Planet struct { Name string `json:"name"` Aphelion float64 `json:"aphelion"` // in million km Perihelion float64 `json:"perihelion"`…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
244
votes
6 answers

How to point Go module dependency in go.mod to a latest commit in a repo?

Starting with v1.11 Go added support for modules. Commands go mod init go build would generate go.mod and go.sum files that contain all found versions for the package dependencies. If a module does not have any releases, the latest…
dimus
  • 8,712
  • 10
  • 45
  • 56
243
votes
6 answers

Stack vs heap allocation of structs in Go, and how they relate to garbage collection

I'm experiencing a bit of cognitive dissonance between C-style stack-based programming, where automatic variables live on the stack and allocated memory lives on the heap, and Python-style stack-based-programming, where the only thing that lives on…
Joe
  • 46,419
  • 33
  • 155
  • 245
241
votes
12 answers

How to properly seed random number generator

I am trying to generate a random string in Go and here is the code I have written so far: package main import ( "bytes" "fmt" "math/rand" "time" ) func main() { fmt.Println(randomString(10)) } func randomString(l int) string…
copperMan
  • 2,435
  • 2
  • 14
  • 5
240
votes
8 answers

Import cycle not allowed

I have a problem with import cycle not allowed It appears when I am trying to test my controller. Here is the output: can't load package: import cycle not allowed package project/controllers/account imports project/controllers/base imports…
softshipper
  • 32,463
  • 51
  • 192
  • 400
238
votes
14 answers

"undefined" function declared in another file?

I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go: package main func main() { emp := NewEmployee() …
Juan M
  • 4,063
  • 4
  • 19
  • 28
236
votes
5 answers

Force retesting or disable test caching

Problem: When I run the same go test twice the second run is not done at all. The results are the cached ones from the first run. PASS ok tester/apitests (cached) Links I already checked https://golang.org/cmd/go/#hdr-Testing_flags but…
Simon Frey
  • 2,539
  • 2
  • 11
  • 20
232
votes
3 answers

Value receiver vs. pointer receiver

It is very unclear for me in which case I would want to use a value receiver instead of always using a pointer receiver. To recap from the docs: type T struct { a int } func (tv T) Mv(a int) int { return 0 } // value receiver func (tp…
Chrisport
  • 2,936
  • 3
  • 15
  • 19
231
votes
13 answers

The maximum value for an int type in Go

How does one specify the maximum value representable for an unsigned integer type? I would like to know how to initialize min in the loop below that iteratively computes min and max lengths from some structs. var minLen uint = ??? var maxLen uint =…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
231
votes
4 answers

Golang tests in sub-directory

I want to create a package in Go with tests and examples for the package as subdirectories to keep the workspace cleaner. Is this possible and if so how? All the documentation always puts the testing code in the same place as the other code, is…
The Graceful Penguin
  • 2,424
  • 2
  • 14
  • 7
231
votes
7 answers

Application auto build versioning

Is it possible to increment a minor version number automatically each time a Go app is compiled? I would like to set a version number inside my program, with an autoincrementing section: $ myapp -version MyApp version 0.5.132 Being 0.5 the version…
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
229
votes
2 answers

Go test string contains substring

How do I check if a string is a substring of another string in Go? For example, I want to check someString.contains("something").
Elliott Beach
  • 10,459
  • 9
  • 28
  • 41
229
votes
4 answers

Default value in Go's method

Is there a way to specify default value in Go's function? I am trying to find this in the documentation but I can't find anything that specifies that this is even possible. func SaySomething(i string = "Hello")(string){ ... }
denniss
  • 17,229
  • 26
  • 92
  • 141