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

How to serve up a JSON response using Go?

Question: Currently I'm printing out my response in the func Index like this fmt.Fprintf(w, string(response)) however, how can I send JSON properly in the request so that it maybe consumed by a view? package main import ( "fmt" …
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
160
votes
8 answers

Cross compile Go on OSX?

I am trying to cross-compile a go app on OSX to build binaries for windows and linux. I have read everything what I could find on the net. Closest example that I have found has been published on (apart from many unfinished discussions on go-nuts…
ljgww
  • 3,418
  • 6
  • 19
  • 21
159
votes
9 answers

Get current time as formatted string in Go?

What's the best way to get the current timestamp in Go and convert to string? I need both date and time in eg. YYYYMMDDhhmmss format.
brianoh
  • 3,407
  • 6
  • 23
  • 23
159
votes
6 answers

Convert an integer to a float number

How do I convert an integer value to float64 type? I tried float(integer_value) But this does not work. And can't find any package that does this on Golang.org How do I get float64 values from integer values?
user2671513
158
votes
7 answers

SSL is not enabled on the server

Trying to communicate with a postgres database with go, preparing the statement like this: var stmt *sql.Stmt var err error stmt, err = db.Prepare(selectStatement) if err != nil { fmt.Printf("db.Prepare error: %v\n",err) return…
Gustavo Semião-Lobo
  • 2,468
  • 3
  • 18
  • 26
158
votes
7 answers

how to listen to N channels? (dynamic select statement)

to start an endless loop of executing two goroutines, I can use the code below: after receiving the msg it will start a new goroutine and go on for ever. c1 := make(chan string) c2 := make(chan string) go DoStuff(c1, 5) go DoStuff(c2, 2) for ;…
JohnSmith
  • 4,538
  • 9
  • 25
  • 25
158
votes
10 answers

How to index characters in a Golang string?

How to get an "E" output rather than 69? package main import "fmt" func main() { fmt.Print("HELLO"[1]) } Does Golang have function to convert a char to byte and vice versa?
user977828
  • 7,259
  • 16
  • 66
  • 117
156
votes
8 answers

How to determine an interface{} value's "real" type?

I have not found a good resource for using interface{} types. For example package main import "fmt" func weirdFunc(i int) interface{} { if i == 0 { return "zero" } return i } func main() { var i = 5 var w =…
cc young
  • 18,939
  • 31
  • 90
  • 148
156
votes
3 answers

Why does Go have a "goto" statement?

I was surprised to find that Go has a 'goto' statement. I've always been taught that 'goto' statements are a thing of the past and evil for it occludes the actual flow of a program, and that functions or methods are always a better way of…
harm
  • 10,045
  • 10
  • 36
  • 41
155
votes
5 answers

What could happen if I don't close response.Body?

In Go, I have some http responses and I sometimes forget to call: resp.Body.Close() What happens in this case? will there be a memory leak? Also is it safe to put in defer resp.Body.Close() immediately after getting the response object? client :=…
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112
155
votes
15 answers

How do I import a specific version of a package using go get?

coming from a Node environment I used to install a specific version of a vendor lib into the project folder (node_modules) by telling npm to install that version of that lib from the package.json or even directly from the console, like so: $ npm…
Wilk
  • 7,873
  • 9
  • 46
  • 70
154
votes
8 answers

How to stop a goroutine

I have a goroutine that calls a method, and passes returned value on a channel: ch := make(chan int, 100) go func(){ for { ch <- do_stuff() } }() How do I stop such a goroutine?
Łukasz Gruner
  • 2,929
  • 3
  • 26
  • 28
154
votes
5 answers

In Go how to get a slice of values from a map?

If I have a map m is there a better way of getting a slice of the values v than this? package main import ( "fmt" ) func main() { m := make(map[int]string) m[1] = "a" m[2] = "b" m[3] = "c" m[4] = "d" // Can this be done…
masebase
  • 4,915
  • 3
  • 24
  • 20
153
votes
4 answers

Partly JSON unmarshal into a map in Go

My websocket server will receive and unmarshal JSON data. This data will always be wrapped in an object with key/value pairs. The key-string will act as value identifier, telling the Go server what kind of value it is. By knowing what type of value,…
ANisus
  • 74,460
  • 29
  • 162
  • 158
152
votes
24 answers

exec: "gcc": executable file not found in %PATH% when trying go build

I am using Windows 10. When I tried to build Chaincode it reported this error # github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11 exec: "gcc": executable file not found in %PATH% My chaincode imports: import ( "fmt" "strconv" …
jaswanth
  • 1,759
  • 2
  • 10
  • 16