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
324
votes
23 answers

How to delete an element from a Slice in Golang

fmt.Println("Enter position to delete::") fmt.Scanln(&pos) new_arr := make([]int, (len(arr) - 1)) k := 0 for i := 0; i < (len(arr) - 1); { if i != pos { new_arr[i] = arr[k] k++ i++ } else { k++ } } for i…
Anchal Sarraf
  • 3,443
  • 2
  • 14
  • 13
322
votes
9 answers

Handling JSON Post Request in Go

So I have the following, which seems incredibly hacky, and I've been thinking to myself that Go has better designed libraries than this, but I can't find an example of Go handling a POST request of JSON data. They are all form POSTs. Here is an…
TomJ
  • 5,389
  • 4
  • 26
  • 31
320
votes
10 answers

What is a rune?

What is a rune in Go? I've been googling but Golang only says in one line: rune is an alias for int32. But how come integers are used all around like swapping cases? The following is a function swapcase. What is all the <= and -? And why doesn't…
user2671513
319
votes
8 answers

How to run test cases in a specified file?

My package test cases are scattered across multiple files, if I run go test it runs all test cases in the package. It is unnecessary to run all of them though. Is there a way to specify a file for go test to run, so that it only runs…
user972946
309
votes
2 answers

Decoding JSON using json.Unmarshal vs json.NewDecoder.Decode

I'm developing an API client where I need to encode a JSON payload on request and decode a JSON body from the response. I've read the source code from several libraries and from what I have seen, I have basically two possibilities for encoding and…
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
308
votes
5 answers

Converting Go struct to JSON

I am trying to convert a Go struct to JSON using the json package but all I get is {}. I am certain it is something totally obvious but I don't see it. package main import ( "fmt" "encoding/json" ) type User struct { name…
magiconair
  • 6,659
  • 4
  • 29
  • 26
307
votes
10 answers

Type converting slices of interfaces

I'm curious why Go does't implicitly convert []T to []interface{} when it will implicitly convert T to interface{}. Is there something non-trivial about this conversion that I'm missing? Example: func foo([]interface{}) { /* do something */…
danny
  • 10,103
  • 10
  • 50
  • 57
303
votes
4 answers

golang why don't we have a set datastructure

I'm trying to solve "The go programming lanaguage" exercise #1.4 which requires me to have a set. I can create a set type but why doesn't the language come with one ? go, having come from google, where guava also originated, why didn't the language…
anjanb
  • 12,999
  • 18
  • 77
  • 106
297
votes
5 answers

What does an underscore in front of an import statement mean?

In this code from go-sqlite3: import ( "database/sql" "fmt" _ "github.com/mattn/go-sqlite3" "log" "os" ) what does the underscore in the import statement mean?
Adrian
  • 19,440
  • 34
  • 112
  • 219
297
votes
11 answers

Constructors in Go

I have a struct and I would like it to be initialised with some sensible default values. Typically, the thing to do here is to use a constructor but since go isn't really OOP in the traditional sense these aren't true objects and it has no…
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
294
votes
7 answers

List directory in Go

I've been trying to figure out how to simply list the files and folders in a single directory in Go. I've found filepath.Walk, but it goes into sub-directories automatically, which I don't want. All of my other searches haven't turned anything…
Behram Mistree
  • 4,088
  • 3
  • 19
  • 21
284
votes
17 answers

What's the proper way to "go get" a private repository?

I'm searching for the way to get $ go get work with private repository, after many google try. The first try: $ go get -v gitlab.com/secmask/awserver-go Fetching https://gitlab.com/secmask/awserver-go?go-get=1 https fetch failed. Fetching…
secmask
  • 7,649
  • 5
  • 35
  • 52
284
votes
13 answers

Is there a way to iterate over a range of integers?

Go's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this: for i := range [1..10] { fmt.Println(i) } Or is there a way to represent range of integers in Go like…
Vishnu
  • 4,377
  • 7
  • 26
  • 40
282
votes
11 answers

Is it possible to capture a Ctrl+C signal (SIGINT) and run a cleanup function, in a "defer" fashion?

I want to capture the Ctrl+C (SIGINT) signal sent from the console and print out some partial run totals.
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
281
votes
3 answers

How to `go test` all tests in my project?

The go test command covers *_test.go files in only one dir. I want to go test the whole project, which means the test should cover all *_test.go files in the dir ./ and every children tree dir under the dir ./. What's the command to do this?
hardPass
  • 19,033
  • 19
  • 40
  • 42