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
211
votes
21 answers

Is assert evil?

The Go language creators write: Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. Proper error handling…
Frank
  • 64,140
  • 93
  • 237
  • 324
211
votes
8 answers

Is there a way to do repetitive tasks at intervals?

Is there a way to do repetitive background tasks in Go? I'm thinking of something like Timer.schedule(task, delay, period) in Java. I know I can do this with a goroutine and Time.sleep(), but I'd like something that easily stopped. Here's what I…
Steve Brisk
  • 2,503
  • 2
  • 16
  • 13
211
votes
7 answers

Does a break statement break from a switch/select?

I know that switch/select statements break automatically after every case. I am wondering, in the following code: for { switch sometest() { case 0: dosomething() case 1: break default: dosomethingelse() …
Matt
  • 21,026
  • 18
  • 63
  • 115
208
votes
7 answers

How to compare if two structs, slices or maps are equal?

I want to check if two structs, slices and maps are equal. But I'm running into problems with the following code. See my comments at the relevant lines. package main import ( "fmt" "reflect" ) type T struct { X int Y string Z…
leiyonglin
  • 6,474
  • 12
  • 36
  • 41
206
votes
10 answers

Go << and >> operators

Could someone please explain to me the usage of << and >> in Go? I guess it is similar to some other languages.
brianoh
  • 2,079
  • 2
  • 13
  • 3
206
votes
7 answers

How are people managing authentication in Go?

For those building RESTful APIs and JS front-end apps in Go, how are you managing authentication? Are you using any particular libraries or techniques? I'm surprised to find so little discussion about this. I keep in mind answers like the following,…
SexxLuthor
  • 4,460
  • 3
  • 18
  • 25
205
votes
8 answers

How to check for an empty struct?

I define a struct ... type Session struct { playerId string beehive string timestamp time.Time } Sometimes I assign an empty session to it (because nil is not possible) session = Session{}; Then I want to check, if it is empty: if…
Michael
  • 6,823
  • 11
  • 54
  • 84
205
votes
4 answers

Is it safe to remove selected keys from map within a range loop?

How can one remove selected keys from a map? Is it safe to combine delete() with range, as in the code below? package main import "fmt" type Info struct { value string } func main() { table := make(map[string]*Info) for i := 0; i <…
Everton
  • 12,589
  • 9
  • 47
  • 59
204
votes
9 answers

How to use custom packages

I'm trying to create and use a custom package in Go. It's probably something very obvious but I cannot find much information about this. Basically, I have these two files in the same folder: mylib.go package mylib type SomeType struct…
laurent
  • 88,262
  • 77
  • 290
  • 428
203
votes
7 answers

From io.Reader to string in Go

I have an io.ReadCloser object (from an http.Response object). What's the most efficient way to convert the entire stream to a string object?
djd
  • 4,988
  • 2
  • 25
  • 35
203
votes
3 answers

Can I list all standard Go packages?

Is there a way in Go to list all the standard/built-in packages (i.e., the packages which come installed with a Go installation)? I have a list of packages and I want to figure out which packages are standard.
Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37
203
votes
2 answers

What is the usage of backtick in golang structs definition?

type NetworkInterface struct { Gateway string `json:"gateway"` IPAddress string `json:"ip"` IPPrefixLen int `json:"ip_prefix_len"` MacAddress string `json:"mac"` ... } I'm quite…
harryz
  • 4,980
  • 6
  • 31
  • 34
203
votes
6 answers

How to declare a constant map in Golang?

I am trying to declare to constant in Go, but it is throwing an error. This is my code: const myMap = map[int]string{ 1: "one", 2: "two", 3: "three", } This is the error map[int]string{…} (value of type map[int]string) is not constant
samol
  • 18,950
  • 32
  • 88
  • 127
202
votes
4 answers

Change values while iterating

Let's suppose I have these types: type Attribute struct { Key, Val string } type Node struct { Attr []Attribute } and that I want to iterate on my node's attributes to change them. I would have loved to be able to do: for _, attr := range…
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
201
votes
9 answers

How to call function from another file in Go

I want to call function from another file in Go. Can any one help? test1.go package main func main() { demo() } test2.go package main import "fmt" func main() { } func demo() { fmt.Println("HI") } How to call demo in test2 from test1?
user1788542