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

Error message "go: go.mod file not found in current directory or any parent directory; see 'go help modules'"

I just updated to the new version of Go - Go version 1.16.2 Linux/amd64 and am getting an error when I build a Hello, World! example: go: go.mod file not found in current directory or any parent directory; see 'go help modules' Even when I follow…
shwick
  • 4,277
  • 6
  • 21
  • 28
200
votes
3 answers

How to add new methods to an existing type in Go?

I want to add a convenience util method on to gorilla/mux Route and Router types: package util import( "net/http" "github.com/0xor1/gorillaseed/src/server/lib/mux" ) func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{ …
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112
200
votes
10 answers

How to initialize a nested struct?

I cannot figure out how to initialize a nested struct. Find an example here: http://play.golang.org/p/NL6VXdHrjh package main type Configuration struct { Val string Proxy struct { Address string Port string …
sontags
  • 3,001
  • 3
  • 19
  • 25
200
votes
5 answers

How to get JSON response from http.Get

I'm trying read JSON data from web, but that code returns empty result. I'm not sure what I'm doing wrong here. package main import "os" import "fmt" import "net/http" import "io/ioutil" import "encoding/json" type Tracks struct { Toptracks…
Akshay Deep Giri
  • 3,139
  • 5
  • 25
  • 33
200
votes
15 answers

How to measure test coverage in Go

Has anyone succeeded in generating code coverage for Go unit tests? I can't find a tool for that on the web.
Georgi Atsev
  • 2,775
  • 2
  • 16
  • 18
199
votes
2 answers

" is pointer to interface, not interface" confusion

I have this problem which seems a bit weird to me. Take a look at this snippet of code: package coreinterfaces type FilterInterface interface { Filter(s *string) bool } type FieldFilter struct { Key string Val string } func (ff…
0rka
  • 2,246
  • 2
  • 11
  • 20
197
votes
4 answers

What is a concise way to create a 2D slice in Go?

I am learning Go by going through A Tour of Go. One of the exercises there asks me to create a 2D slice of dy rows and dx columns containing uint8. My current approach, which works, is this: a:= make([][]uint8, dy) // initialize a slice of dy…
hazrmard
  • 3,397
  • 4
  • 22
  • 36
197
votes
2 answers

What do three dots "./..." mean in Go command line invocations?

If you run Golang tests on Travis CI, it will download all of your dependencies with three dots: go get -d -v ./... && go build -v ./... What does ./... indicate or expand to there? I've done some research but it doesn't seem to be a Unix…
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
196
votes
8 answers

Mock functions in Go

I'm puzzled with dependencies. I want to be able to replace some function calls with mock ones. Here's a snippet of my code: func get_page(url string) string { get_dl_slot(url) defer free_dl_slot(url) resp, err := http.Get(url) …
GolDDranks
  • 3,272
  • 4
  • 22
  • 30
196
votes
2 answers

How to define multiple name tags in a struct

I need to get an item from a mongo database, so I defined a struct like this type Page struct { PageId string `bson:"pageId"` Meta map[string]interface{} `bson:"meta"` } Now I also need to encode it to JSON, but it encodes…
scc
  • 10,342
  • 10
  • 51
  • 65
195
votes
3 answers

Go naming conventions for const

I'm trying to determine whether there is a naming convention for the names of const in Golang. I personally would tend to follow the C style and write them in upper case, but I haven't found anything on this page…
LtWorf
  • 7,286
  • 6
  • 31
  • 45
193
votes
4 answers

How do I do a literal *int64 in Go?

I have a struct type with a *int64 field. type SomeType struct { SomeField *int64 } At some point in my code, I want to declare a literal of this (say, when I know said value should be 0, or pointing to a 0, you know what I mean) instance :=…
ThisGuy
  • 2,661
  • 2
  • 18
  • 18
192
votes
3 answers

What does the '.' (dot or period) in a Go import statement do?

In the Go tutorial, and most of the Go code I've looked at, packages are imported like this: import ( "fmt" "os" "launchpad.net/lpad" ... ) But in http://bazaar.launchpad.net/~niemeyer/lpad/trunk/view/head:/session_test.go, the…
Eric Seidel
  • 2,781
  • 2
  • 17
  • 18
192
votes
15 answers

Is there a method to generate a UUID with Go language?

I have code that looks like this: u := make([]byte, 16) _, err := rand.Read(u) if err != nil { return } u[8] = (u[8] | 0x80) & 0xBF // what does this do? u[6] = (u[6] | 0x40) & 0x4F // what does this do? return hex.EncodeToString(u) It…
hardPass
  • 19,033
  • 19
  • 40
  • 42
190
votes
4 answers

Split a string on whitespace in Go?

Given an input string such as " word1 word2 word3 word4 ", what would be the best approach to split this as an array of strings in Go? Note that there can be any number of spaces or unicode-spacing characters between each word. In Java I would…
ralfoide
  • 2,705
  • 4
  • 23
  • 21