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
540
votes
13 answers

Reading a file line by line in Go

I'm unable to find file.ReadLine function in Go. How does one read a file line by line?
g06lin
  • 5,725
  • 3
  • 18
  • 13
535
votes
3 answers

Function declaration syntax: things in parenthesis before function name

I'm sorry I couldn't be more specific in the question title, but I was reading some Go code and I encountered function declarations of this form: func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ... } from…
530
votes
10 answers

How to assign string to bytes array

I want to assign string to bytes array: var arr [20]byte str := "abc" for k, v := range []byte(str) { arr[k] = byte(v) } Have another method?
sofire
  • 5,417
  • 2
  • 14
  • 4
525
votes
11 answers

When is the init() function run?

I've tried to find a precise explanation of what the init() function does in Go. I read what Effective Go says but I was unsure if I understood fully what it said. The exact sentence I am unsure is the following: And finally means finally: init is…
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
521
votes
5 answers

Pointers vs. values in parameters and return values

In Go there are various ways to return a struct value or slice thereof. For individual ones I've seen: type MyStruct struct { Val int } func myfunc() MyStruct { return MyStruct{Val: 1} } func myfunc() *MyStruct { return…
Zef Hemel
  • 6,457
  • 3
  • 20
  • 14
483
votes
9 answers

How to multiply duration by integer?

To test concurrent goroutines, I added a line to a function to make it take a random time to return (up to one second) time.Sleep(rand.Int31n(1000) * time.Millisecond) However when I compiled, I got this error .\crawler.go:49: invalid operation:…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
470
votes
11 answers

What is the best way to test for an empty string in Go?

Which method is best (most idomatic) for testing non-empty strings (in Go)? if len(mystring) > 0 { } Or: if mystring != "" { } Or something else?
Richard
  • 10,122
  • 10
  • 42
  • 61
469
votes
19 answers

How to generate a random string of a fixed length in Go?

I want a random string of characters only (uppercase or lowercase), no numbers, in Go. What is the fastest and simplest way to do this?
Anish Shah
  • 7,669
  • 8
  • 29
  • 40
469
votes
9 answers

Does Go have "if x in" construct similar to Python?

How can I check if x is in an array without iterating over the entire array, using Go? Does the language have a construct for this? Like in Python: if "x" in array: # do something
user1529891
462
votes
8 answers

Difference between := and = operators in Go

What is the difference between the = and := operators, and what are the use cases for them? They both seem to be for an assignment?
Chris
  • 8,168
  • 8
  • 36
  • 51
446
votes
19 answers

Contains method for a slice

Is there anything similar to a slice.contains(object) method in Go without having to do a search through each element in a slice?
vosmith
  • 4,948
  • 2
  • 18
  • 26
445
votes
25 answers

What should be the values of GOPATH and GOROOT?

I'm trying to install doozer like this: $ goinstall github.com/ha/doozer I get these errors. goinstall: os: go/build: package could not be found locally goinstall: fmt: go/build: package could not be found locally goinstall: io: go/build: package…
jshen
  • 11,507
  • 7
  • 37
  • 59
442
votes
6 answers

Iterating over all the keys of a map

Is there a way to get a list of all the keys in a Go language map? The number of elements is given by len(), but if I have a map like: m := map[string]string{ "key1":"val1", "key2":"val2" }; How do I iterate over all the keys?
Martin Redmond
  • 13,366
  • 6
  • 36
  • 32
436
votes
5 answers

Convert string to integer type in Go?

I'm trying to convert a string returned from flag.Arg(n) to an int. What is the idiomatic way to do this in Go?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
434
votes
13 answers

Checking the equality of two slices

How can I check if two slices are equal, given that the operators == and != are not an option? package main import "fmt" func main() { s1 := []int{1, 2} s2 := []int{1, 2} fmt.Println(s1 == s2) } This does not compile with: invalid…
wei2912
  • 6,141
  • 2
  • 19
  • 20