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
431
votes
12 answers

How to avoid annoying error "declared and not used"

I'm learning Go but I feel it is a bit annoying that when compiling, I should not leave any variable or package unused. This is really quite slowing me down. For example, I just wanted to declare a new package and plan to use it later or just…
A-letubby
  • 8,474
  • 8
  • 38
  • 48
410
votes
10 answers

Getting a slice of keys from a map

Is there any simpler/nicer way of getting a slice of keys from a map in Go? Currently I am iterating over the map and copying the keys to a slice: i := 0 keys := make([]int, len(mymap)) for k := range mymap { keys[i] = k i++ }
Saswat Padhi
  • 6,044
  • 4
  • 20
  • 25
409
votes
5 answers

Correct way to initialize empty slice

To declare an empty slice, with a non-fixed size, is it better to do: mySlice1 := make([]int, 0) or: mySlice2 := []int{} Just wondering which one is the correct way.
eouti
  • 5,338
  • 3
  • 34
  • 42
405
votes
8 answers

Removing packages installed with go get

I ran go get package to download a package before learning that I needed to set my GOPATH otherwise that package sullies my root Go install (I would much prefer to keep my Go install clean and separate core from custom). How do I remove packages…
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
403
votes
17 answers

go get results in 'terminal prompts disabled' error for github private repo

I created the private repo examplesite/myprivaterepo using the Github UI from my browser. Then I went to my go directory (on the desktop) and cloned it: $ cd $GOPATH $ go get github.com/examplesite/myprivaterepo So far so good. Created the file…
tomcam
  • 4,625
  • 3
  • 19
  • 22
393
votes
4 answers

X does not implement Y (... method has a pointer receiver)

There are already several Q&As on this "X does not implement Y (... method has a pointer receiver)" thing, but to me, they seems to be talking about different things, and not applying to my specific case. So, instead of making the question very…
xpt
  • 20,363
  • 37
  • 127
  • 216
390
votes
9 answers

How do I send a JSON string in a POST request in Go

I tried working with Apiary and made a universal template to send JSON to mock server and have this code: package main import ( "encoding/json" "fmt" "github.com/jmcvetta/napping" "log" "net/http" ) func main() { url :=…
Ladislav Prskavec
  • 4,351
  • 3
  • 19
  • 16
379
votes
12 answers

How can I read from standard input in the console?

I would like to read standard input from the command line, but my attempts have ended with the program exiting before I'm prompted for input. I'm looking for the equivalent of Console.ReadLine() in C#. This is what I currently have: package…
Dante
  • 10,722
  • 16
  • 51
  • 63
361
votes
10 answers

How to read/write from/to a file using Go

I've been trying to learn Go on my own, but I've been stumped on trying read from and write to ordinary files. I can get as far as inFile, _ := os.Open(INFILE, 0, 0), but actually getting the content of the file doesn't make sense, because the read…
Seth Hoenig
  • 7,047
  • 6
  • 31
  • 30
356
votes
30 answers

How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

I am trying to do a go get: go get github.com/go-sql-driver/mysql and it fails with the following error: package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath when I do a go env , a list of…
David Saintloth
  • 4,061
  • 2
  • 16
  • 16
348
votes
18 answers

How to get the directory of the currently running file?

In nodejs I use __dirname . What is the equivalent of this in Golang? I have googled and found out this article http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/ . Where he uses below code _, filename, _, _ :=…
ekanna
  • 5,462
  • 8
  • 28
  • 31
338
votes
11 answers

Why would I make() or new()?

The introduction documents dedicate many paragraphs to explaining the difference between new() and make(), but in practice, you can create objects within local scope and return them. Why would you use the pair of allocators?
salezica
  • 74,081
  • 25
  • 105
  • 166
337
votes
15 answers

How can I pretty-print JSON using Go?

Does anyone know of a simple way to pretty-print JSON output in Go? I'd like to pretty-print the result of json.Marshal, as well as formatting an existing string of JSON so it's easier to read.
Brad Peabody
  • 10,917
  • 9
  • 44
  • 63
333
votes
13 answers

How to handle configuration in Go

What is the preferred way to handle configuration parameters for a Go program (the kind of stuff one might use properties files or ini files for, in other contexts)?
theglauber
  • 28,367
  • 7
  • 29
  • 47
325
votes
5 answers

Delete key in map

I have a map: var sessions = map[string] chan int{} How do I delete sessions[key]? I tried: sessions[key] = nil,false; That didn't work. Update (November 2011): The special syntax for deleting map entries is removed in Go version 1: Go 1 will…
jonaz
  • 3,764
  • 2
  • 20
  • 20