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
168
votes
2 answers

What does go install do?

The docs say nothing about what build vs install does My expectation was that it's like make install; i.e. it takes the compiled stuff and puts in its final location (/usr/local/bin/my_new_toy or whatever) but it seems that it puts things in…
pm100
  • 48,078
  • 23
  • 82
  • 145
168
votes
5 answers

How to wait for all goroutines to finish without using time.Sleep?

This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the file is printed out). How do I avoid using the…
Dante
  • 10,722
  • 16
  • 51
  • 63
167
votes
19 answers

How to install the current version of Go in Ubuntu Precise

Running sudo apt-get install golang-stable, I get Go version go1.0.3. Is there any way to install go1.1.1?
scc
  • 10,342
  • 10
  • 51
  • 65
167
votes
9 answers

How to split a string and assign it to variables

In Python it is possible to split a string and assign it to variables: ip, port = '127.0.0.1:5432'.split(':') but in Go it does not seem to work: ip, port := strings.Split("127.0.0.1:5432", ":") // assignment count mismatch: 2 = 1 Question: How to…
Pyt
  • 1,925
  • 2
  • 13
  • 10
166
votes
4 answers

How to convert a bool to a string in Go?

I am trying to convert a bool called isExist to a string (true or false) by using string(isExist) but it does not work. What is the idiomatic way to do this in Go?
Casper
  • 4,435
  • 10
  • 41
  • 72
165
votes
2 answers

What does go build build? (go build vs. go install)

New Go programmers often don't know or get confused what the fundamental go build command does. What do exactly the go build and go install commands build and where do they put the result/output?
icza
  • 389,944
  • 63
  • 907
  • 827
165
votes
4 answers

Proper package naming for testing with the Go language

I have seen several different test package naming strategies within Go and wanted to know what pros and cons of each are and which one I should use. Strategy 1: File name: github.com/user/myfunc.go package myfunc Test file name:…
Dan
  • 5,013
  • 5
  • 33
  • 59
164
votes
12 answers

Go install fails with error: no install location for directory xxx outside GOPATH

~/src/go-statsd-client> echo $GOPATH /Users/me/gopath ~/src/go-statsd-client> echo $GOROOT /usr/local/Cellar/go/1.1.1\ ~/src/go-statsd-client> go install go install: no install location for directory /Users/me/src/go-statsd-client outside GOPATH No…
tgandrews
  • 12,349
  • 15
  • 43
  • 55
164
votes
4 answers

Why can't I assign a *Struct to an *Interface?

I'm just working through the Go tour, and I'm confused about pointers and interfaces. Why doesn't this Go code compile? package main type Interface interface {} type Struct struct {} func main() { var ps *Struct var pi *Interface pi =…
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
163
votes
2 answers

Go Interface Fields

I'm familiar with the fact that, in Go, interfaces define functionality, rather than data. You put a set of methods into an interface, but you are unable to specify any fields that would be required on anything that implements that interface. For…
Matt Mc
  • 8,882
  • 6
  • 53
  • 89
163
votes
5 answers

How do you clear a slice in Go?

What is the appropriate way to clear a slice in Go? Here's what I've found in the go forums: // test.go package main import ( "fmt" ) func main() { letters := []string{"a", "b", "c", "d"} fmt.Println(cap(letters)) …
Chris Weber
  • 5,555
  • 8
  • 44
  • 52
162
votes
4 answers

Call Go functions from C

I am trying to create a static object written in Go to interface with a C program (say, a kernel module or something). I have found documentation on calling C functions from Go, but I haven't found much on how to go the other way. What I've found…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
162
votes
12 answers

How to do one-liner if else statement?

Please see https://golangdocs.com/ternary-operator-in-golang as pointed by @accdias (see comments) Can I write a simple if-else statement with variable assignment in go (golang) as I would do in php? For example: $var = ( $a > $b )? $a:…
thoroc
  • 3,291
  • 2
  • 27
  • 34
162
votes
15 answers

cannot download, $GOPATH not set

I want to install json2csv using go get github.com/jehiah/json2csv but I receive this error: package github.com/jehiah/json2csv: cannot download, $GOPATH not set. For more details see: go help go path Any help on how to fix this on MacOS?
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
162
votes
6 answers

In Go's http package, how do I get the query string on a POST request?

I'm using the httppackage from Go to deal with POST request. How can I access and parse the content of the query string from the Requestobject ? I can't find the answer from the official documentation.
Fabien
  • 12,486
  • 9
  • 44
  • 62