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
280
votes
15 answers

Removing fields from struct or hiding them in JSON Response

I've created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller. I'd now like to allow the caller to be able to select the specific fields…
user387049
  • 6,647
  • 8
  • 53
  • 55
280
votes
6 answers

How can I read a whole file into a string variable

I have lots of small files, I don't want to read them line by line. Is there a function in Go that will read a whole file into a string variable?
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
277
votes
2 answers

Convert byte slice to io.Reader

In my project, I have a byte slice from a request's response. defer resp.Body.Close() if resp.StatusCode != http.StatusOK { log.Println("StatusCode为" + strconv.Itoa(resp.StatusCode)) return } respByte, err := ioutil.ReadAll(resp.Body) if…
Chan Willson
  • 2,803
  • 2
  • 13
  • 6
274
votes
13 answers

Does Go provide REPL?

The interactive environment is VERY helpful for a programmer. However, it seems Go does not provide it. Is my understanding correct?
z_axis
  • 8,272
  • 7
  • 41
  • 61
273
votes
4 answers

How to set headers in http get request?

I'm doing a simple http GET in Go: client := &http.Client{} req, _ := http.NewRequest("GET", url, nil) res, _ := client.Do(req) But I can't found a way to customize the request header in the doc, thanks
wong2
  • 34,358
  • 48
  • 134
  • 179
267
votes
4 answers

What is the `zero` value for time.Time in Go?

In an error condition, I tried to return nil, which throws the error: cannot use nil as type time.Time in return argument What is the zero value for time.Time?
Mingyu
  • 31,751
  • 14
  • 55
  • 60
265
votes
8 answers

How do you print in a Go test using the "testing" package?

I'm running a test in Go with a statement to print something (i.e. for debugging of tests) but it's not printing anything. func TestPrintSomething(t *testing.T) { fmt.Println("Say hi") } When I run go test on this file, this is the output: ok …
platwp
  • 3,025
  • 4
  • 15
  • 10
262
votes
9 answers

How to set default values in Go structs

There are multiple answers/techniques to the below question: How to set default values to golang structs? How to initialize structs in golang I have a couple of answers but further discussion is required.
Prateek
  • 6,644
  • 6
  • 22
  • 26
261
votes
5 answers

Declare a constant array

I have tried: const ascii = "abcdefghijklmnopqrstuvwxyz" const letter_goodness []float32 = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276,…
ceth
  • 44,198
  • 62
  • 180
  • 289
258
votes
8 answers

Is it OK to leave a channel open?

Is it OK to leave a Go channel open forever (never close the channel) if I never check for its state? Will it lead to memory leaks? Is the following code OK? func (requestCh chan<- Request) GetResponse(data RequestData) Response { reply :=…
Kluyg
  • 5,119
  • 2
  • 25
  • 28
257
votes
7 answers

Organizing a multiple-file Go project

Note: this question is related to this one, but two years is a very long time in Go history. What is the standard way to organize a Go project during development ? My project is a single package mypack, so I guess I put all the .go files in a mypack…
Blacksad
  • 14,906
  • 15
  • 70
  • 81
257
votes
4 answers

How to get the last element of a slice?

What is the Go way for extracting the last element of a slice? var slice []int slice = append(slice, 2) slice = append(slice, 7) slice[len(slice)-1:][0] // Retrieves the last element The solution above works, but seems awkward.
Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
256
votes
9 answers

How does Go update third-party packages?

Looking how actively golang packages grow and improve I wonder how the problem with package versions is solved? I see that one way is to store third-party packages under a project folder. But what if I install it with go get?
shalakhin
  • 4,586
  • 5
  • 25
  • 30
253
votes
5 answers

cannot convert data (type interface {}) to type string: need type assertion

I am pretty new to go and I was playing with this notify package. At first I had code that looked like this: func doit(w http.ResponseWriter, r *http.Request) { notify.Post("my_event", "Hello World!") fmt.Fprint(w, "+OK") } I wanted to…
Alfred
  • 60,935
  • 33
  • 147
  • 186
252
votes
8 answers

Can functions be passed as parameters?

In Java I can do something like derp(new Runnable { public void run () { /* run this sometime later */ } }) and "run" the code in the method later. It's a pain to handle (anonymous inner class), but it can be done. Does Go have something that can…
Saad
  • 26,316
  • 15
  • 48
  • 69