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
152
votes
5 answers

How can I efficiently download a large file using Go?

Is there a way to download a large file using Go that will store the content directly into a file instead of storing it all in memory before writing it to a file? Because the file is so big, storing it all in memory before writing it to a file is…
Cory
  • 14,865
  • 24
  • 57
  • 72
151
votes
5 answers

Runtime error: assignment to entry in nil map

I am trying to generate a map and then convert that to a yaml file like this: uid : kasi: cn: Chaithra street: fkmp nandan: cn: Chaithra street: fkmp remya: cn: Chaithra street:…
Kasinath Kottukkal
  • 2,471
  • 4
  • 22
  • 28
151
votes
8 answers

Is there a way to iterate over a slice in reverse in Go?

It would be convenient to be able to say something like: for _, element := reverse range mySlice { ... } Edit: I asked this question a long time ago, it is 2022 now and the generic solution by @Ivan below seems like the way to go!
agam
  • 5,064
  • 6
  • 31
  • 37
151
votes
1 answer

"used as value" in function call

What's the proper way of calling functions when evaluating their values in conditional statements? package main import "fmt" func main(){ if sumThis(1,2) > sumThis(3,4){ fmt.Println("test") } else { …
Saad
  • 26,316
  • 15
  • 48
  • 69
150
votes
3 answers

Go doing a GET request and building the Querystring

I am pretty new to Go and don't quite understand everything as yet. In many of the modern languages Node.js, Angular, jQuery, PHP you can do a GET request with additional query string parameters. Doing this in Go isn't quite a simple as it seems,…
Rudi Strydom
  • 4,417
  • 5
  • 21
  • 30
150
votes
4 answers

Declare slice or make slice?

In Go, what is the difference between var s []int and s := make([]int, 0)? I find that both works, but which one is better?
Wang Yi
  • 1,777
  • 3
  • 13
  • 6
150
votes
7 answers

Correct approach to global logging

What's the pattern for application logging in Go? If I've got, say, 5 goroutines I need to log from, should I... Create a single log.Logger and pass it around? Pass around a pointer to that log.Logger? Should each goroutine or function create a…
Carson
  • 17,073
  • 19
  • 66
  • 87
150
votes
35 answers

How to reverse a string in Go?

How can we reverse a simple string in Go?
user211499
  • 1,537
  • 2
  • 11
  • 6
150
votes
7 answers

ToString() function in Go

The strings.Join function takes slices of strings only: s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) But it would be nice to be able to pass arbitrary objects which implement a ToString() function. type ToStringConverter…
deamon
  • 89,107
  • 111
  • 320
  • 448
149
votes
4 answers

Accessing local packages within a go module (go 1.11)

I'm trying out Go's new modules system and am having trouble accessing local packages. The following project is in a folder on my desktop outside my gopath. My project structure looks like: / - /platform - platform.go - main.go -…
David Alsh
  • 6,747
  • 6
  • 34
  • 60
149
votes
7 answers

How to break a long line of code in Golang?

Coming from Python, I'm not used to see code lines longer than 80 columns. So when I encounter this: err := database.QueryRow("select * from users where user_id=?", id).Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last,…
Karlom
  • 13,323
  • 27
  • 72
  • 116
149
votes
6 answers

Separating unit tests and integration tests in Go

Is there an established best practice for separating unit tests and integration tests in GoLang (testify)? I have a mix of unit tests (which do not rely on any external resources and thus run really fast) and integration tests (which do rely on any…
Craig Jones
  • 2,428
  • 2
  • 17
  • 11
148
votes
7 answers

mkdir if not exists using golang

I am learning golang(beginner) and I have been searching on both google and stackoverflow but I could not find an answer so excuse me if already asked, but how can I mkdir if not exists in golang. For example in node I would use fs-extra with the…
Alfred
  • 60,935
  • 33
  • 147
  • 186
148
votes
8 answers

Delete element in a slice

func main() { a := []string{"Hello1", "Hello2", "Hello3"} fmt.Println(a) // [Hello1 Hello2 Hello3] a = append(a[:0], a[1:]...) fmt.Println(a) // [Hello2 Hello3] } How does this delete trick with the append function work? It…
Jorge Olivero
  • 3,705
  • 9
  • 27
  • 33
148
votes
5 answers

What is the difference between go get and go install?

After playing with the go tool for a while, it looks like go get: (optionally) downloads, compiles, and installs a piece of software, while go install simply compiles and installs it. In this case, why does the go install command exist, since go…
thiagowfx
  • 4,832
  • 6
  • 37
  • 51