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

Ensure a type implements an interface at compile time in Go

How can I ensure that a type implements an interface at compile time? The typical way to do this is by failure to assign to support interfaces from that type, however I have several types that are only converted dynamically. At runtime this…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
29
votes
3 answers

What does "..." mean when next to a parameter in a go function declaration?

I was going through some code written in Google's Go language, and I came across this: func Statusln(a ...interface{}) func Statusf(format string, a ...interface{}) I don't understand what the ... means. Does anybody know?
Chaos
  • 11,213
  • 14
  • 42
  • 69
28
votes
2 answers

convert string to fixed size byte array in Go

Is there convenient way for initial a byte array? package main import "fmt" type T1 struct { f1 [5]byte // I use fixed size here for file format or network packet format. f2 int32 } func main() { t := T1{"abcde", 3} // t:=…
Daniel YC Lin
  • 15,050
  • 18
  • 63
  • 96
28
votes
1 answer

How to pass command-line arguments in debug mode in VSCode with golang

I am having difficulty in passing command-line arguments in VSCode (debug mode) with golang. Below is the small code example and the launch.json: package main import ( "flag" "fmt" ) func main() { flag1Ptr := flag.Bool("flag1", false,…
Dave_J
  • 381
  • 1
  • 3
  • 6
28
votes
4 answers

Why doesn't Go have a function to calculate the absolute value of integers?

In Go, why is there no function which directly calculates absolute value for integer datatypes? Currently all integer values have to be typecast to float64 and then passed to math.Abs(), which returns a float64, which again has to be typecast into…
Piyush Singal
  • 335
  • 1
  • 3
  • 7
28
votes
2 answers

Why does sync.Mutex largely drop performance when goroutine contention is more than 3400?

I am comparing the performance regarding sync.Mutex and Go channels. Here is my benchmark: // go playground: https://play.golang.org/p/f_u9jHBq_Jc const ( start = 300 // actual = start * goprocs end = 600 // actual = end * goprocs …
Changkun
  • 1,502
  • 1
  • 14
  • 29
28
votes
4 answers

Setting timezone globally in golang

I'm trying to modify golang timezone for my application I have took a look at time package, initializing timezone happens in time/zoneinfo_unix.go @ initLocal The function simply tries to read environment variable TZ and if it's valid it loads…
Abdelrahman Magraby
  • 1,053
  • 1
  • 12
  • 16
28
votes
3 answers

Go modules, private repos and gopath

We are converting our internal codebase from the dep dependency manager to go modules (vgo or built in with go1.11.2). Imagine we have code like this: $GOPATH/src/mycompany/myprogram/main.go: package main import ( "fmt" lib…
diagprov
  • 443
  • 1
  • 4
  • 8
28
votes
4 answers

generate godoc documentation for an entire project?

I've been wrestling with godoc and found that "go doc" is more for providing usage help from the command line for instance: go doc -cmd -u lists the package comment and any functions (or other entities) go doc *function* then shows the…
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
28
votes
2 answers

Generate random float64 numbers in specific range using Golang

package main import ( "fmt" "math/rand" "time" ) func genRandNums(min, max float64) []float64 { var randNums []float64 s := rand.NewSource(time.Now().Unix()) r := rand.New(s) for x := 0; x < 10; x++ { //…
Dana
  • 437
  • 1
  • 7
  • 12
28
votes
2 answers

How to extend a Go interface to another interface?

I have a Go interface: type People interface { GetName() string GetAge() string } Now I want another interface Student: 1. type Student interface { GetName() string GetAge() string GetScore() int GetSchoolName()…
linrongbin
  • 2,967
  • 6
  • 31
  • 59
28
votes
2 answers

Is there a way to know who holds a reference to an object in Go?

I am currently trying to debug a nasty memory leak in our Go code. What I know: where memory is going (pprof with -base flag) why new memory is being allocated ("reconnect" feature in our code) number of goroutines is not growing…
melekes
  • 1,880
  • 2
  • 24
  • 30
28
votes
1 answer

What is the purpose of a field named "_" (underscore) containing an empty struct?

I've seen two pieces of Go code using this pattern: type SomeType struct{ Field1 string Field2 bool _ struct{} // <-- what is this? } Can anyone explain what this code accomplishes?
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
28
votes
1 answer

Extend package struct in golang

is it possible in golang extend struct (something like extend a class in other languages, and use it with functions for old one) I have https://github.com/xanzy/go-gitlab/blob/master/services.go#L287 type SetSlackServiceOptions package gitlab //…
JAttanonRadar
  • 463
  • 1
  • 4
  • 11
28
votes
4 answers

panic: json: cannot unmarshal array into Go value of type main.Structure

What are you trying to accomplish? I am trying to parse data from a json api. Paste the part of the code that shows the problem. package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) type Structure…
Peter S
  • 827
  • 1
  • 8
  • 24
1 2 3
99
100