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
179
votes
6 answers

Pair/tuple data type in Go

I need a queue of (string, int) pairs. That's easy enough: type job struct { url string depth int } queue := make(chan job) queue <- job{url, depth} are there built-in pair/tuple data types in Go? There is support for returning multiple…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
178
votes
3 answers

json.Marshal(struct) returns "{}"

type TestObject struct { kind string `json:"kind"` id string `json:"id, omitempty"` name string `json:"name"` email string `json:"email"` } func TestCreateSingleItemResponse(t *testing.T) { testObject := new(TestObject) …
Doug Knesek
  • 6,527
  • 3
  • 21
  • 26
178
votes
4 answers

Subtracting time.Duration from time in Go

I have a time.Time value obtained from time.Now() and I want to get another time which is exactly 1 month ago. I know subtracting is possible with time.Sub() (which wants another time.Time), but that will result in a time.Duration and I need it the…
Martijn van Maasakkers
  • 2,647
  • 3
  • 16
  • 20
178
votes
5 answers

Difference between fmt.Println() and println() in Go

As illustrated below, both fmt.Println() and println() give same output in Go: Hello world! But: how do they differ from each other? Snippet 1, using the fmt package; package main import ( "fmt" ) func main() { fmt.Println("Hello…
YulCheney
  • 2,877
  • 2
  • 18
  • 14
177
votes
6 answers

Go: panic: runtime error: invalid memory address or nil pointer dereference

When running my Go program, it panics and returns the following: panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x38 pc=0x26df] goroutine 1 [running]: main.getBody(0x1cdcd4, 0xf800000004, 0x1f2b44,…
elithrar
  • 23,364
  • 10
  • 85
  • 104
176
votes
4 answers

Go Unpacking Array As Arguments

So in Python and Ruby there is the splat operator (*) for unpacking an array as arguments. In Javascript there is the .apply() function. Is there a way of unpacking an array/slice as function arguments in Go? Any resources for this would be great as…
eatonphil
  • 13,115
  • 27
  • 76
  • 133
175
votes
7 answers

How to set timeout for http.Get() requests in Golang?

I'm making a URL fetcher in Go and have a list of URLs to fetch. I send http.Get() requests to each URL and obtain their response. resp,fetch_err := http.Get(url) How can I set a custom timeout for each Get request? (The default time is very long…
pymd
  • 4,021
  • 6
  • 26
  • 27
173
votes
6 answers

Convert time.Time to string

I'm trying to add some values from my database to a []string in Go. Some of these are timestamps. I get the error: cannot use U.Created_date (type time.Time) as type string in array element Can I convert time.Time to string? type UsersSession…
A.D
  • 2,150
  • 3
  • 13
  • 19
173
votes
2 answers

Indentation in Go: tabs or spaces?

Is there a standard Google Go coding conventions document somewhere that sets whether tabs or spaces are preferred for indentation in Go source code? What is the official recommendation, if any?
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
172
votes
8 answers

How to print the values of slices

I want to see the values which are in the slice. How can I print them? projects []Project
fnr
  • 9,007
  • 5
  • 17
  • 16
171
votes
9 answers

Unmarshaling nested JSON objects

There are a few questions on the topic but none of them seem to cover my case, thus I'm creating a new one. I have JSON like the following: {"foo":{ "bar": "1", "baz": "2" }, "more": "text"} Is there a way to unmarshal the nested bar property and…
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
170
votes
1 answer

Which is the nicer way to initialize a map?

As map is a reference type. What is difference between:? m := make(map[string]int32) and m := map[string]int32{}
iwat
  • 3,591
  • 2
  • 20
  • 24
169
votes
3 answers

What's the recommended way to connect to MySQL from Go?

I am looking for a reliable solution to connect to a MySQL database from Go. I've seen some libraries around, but it is difficult to determine the different states of completeness and current maintenance. I don't have complex needs, but I'd like to…
Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48
169
votes
6 answers

How to check whether a file or directory exists?

I want to check the existence of file ./conf/app.ini in my Go code, but I can't find a good way to do that. I know there is a method of File in Java: public boolean exists(), which returns true if the file or directory exists. But how can this be…
hardPass
  • 19,033
  • 19
  • 40
  • 42
168
votes
4 answers

How to convert interface{} to string?

I'm using docopt to parse command-line arguments. This works, and it results in a map, such as map[:www.google.de :80 --help:false --version:false] Now I would like to concatenate the host and the port value to a string with a colon…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425