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
189
votes
8 answers

Extracting substrings in Go

I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline…
mark2222
  • 2,007
  • 2
  • 12
  • 6
188
votes
11 answers

Why can't I duplicate a slice with `copy()`?

I need to make a copy of a slice in Go and reading the docs there is a copy function at my disposal. The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a …
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
188
votes
8 answers

Iterate through the fields of a struct in Go

Basically, the only way (that I know of) to iterate through the values of the fields of a struct is like this: type Example struct { a_number uint32 a_string string } //... r := &Example{(2 << 31) - 1, "...."}: for _, d:= range…
omninonsense
  • 6,644
  • 9
  • 45
  • 66
187
votes
4 answers

How to print boolean value in Go?

As we have %d for int. What is the format specifier for boolean values?
Anuj Verma
  • 2,519
  • 3
  • 17
  • 23
187
votes
7 answers

How to get the number of characters in a string

How can I get the number of characters of a string in Go? For example, if I have a string "hello" the method should return 5. I saw that len(str) returns the number of bytes and not the number of characters so len("£") returns 2 instead of 1 because…
Ammar
  • 5,070
  • 8
  • 28
  • 27
187
votes
2 answers

How to import and use different packages of the same name

For example, I want to use both text/template and html/template in one source file. But the code below throw errors. import ( "fmt" "net/http" "text/template" // template redeclared as imported package name "html/template" //…
hardPass
  • 19,033
  • 19
  • 40
  • 42
186
votes
6 answers

What are conventions for filenames in Go?

I could find the conventions for naming packages in Go: no underscore between words, everything lowercase. Does this convention apply to the filenames too? Do you also put one struct in one file as if you did for a java class and then name the file…
david
  • 2,467
  • 3
  • 14
  • 13
185
votes
4 answers

Does the Go language have function/method overloading?

I'm porting a C library to Go. A C function (with varargs) is defined like this: curl_easy_setopt(CURL *curl, CURLoption option, ...); So I created wrapper C functions: curl_wrapper_easy_setopt_str(CURL *curl, CURLoption option, char*…
Darius Kucinskas
  • 10,193
  • 12
  • 57
  • 79
185
votes
2 answers

No startswith,endswith functions in Go?

Just curious to findout: why aren't there standard functions like startswith, endswith, etc as part of the standard libraries in the Go programming language?
Sahas
  • 10,637
  • 9
  • 41
  • 51
185
votes
16 answers

Go build: "Cannot find package" (even though GOPATH is set)

Even though I have GOPATH properly set, I still can't get "go build" or "go run" to find my own packages. What am I doing wrong? $ echo $GOROOT /usr/local/go $ echo $GOPATH /home/mitchell/go $ cat ~/main.go package main import "foobar" func main()…
MitchellSalad
  • 4,171
  • 8
  • 23
  • 24
182
votes
27 answers

"package XXX is not in GOROOT" when building a Go project

I have a weird issue that arose when I took a break from this project. Upon starting up Golang, I'm riddled with errors when trying to run my project. The specific error, when building one of my packages, is: start.go: package project/game is not in…
Michael Shum
  • 2,417
  • 2
  • 11
  • 8
181
votes
3 answers

Are maps passed by value or by reference in Go?

Are maps passed by value or reference in Go ? It is always possible to define a function as following, but is this an overkill ? func foo(dat *map[string]interface{}) {...} Same question for return value. Should I return a pointer to the map, or…
chmike
  • 20,922
  • 21
  • 83
  • 106
181
votes
10 answers

How can I do test setup using the testing package in Go

How can I do overall test setup processing which sets the stage for all the tests when using the testing package? As an example in Nunit there is a [SetUp] attribute. [TestFixture] public class SuccessTests { [SetUp] public void Init() { /* Load…
miltonb
  • 6,905
  • 8
  • 45
  • 55
181
votes
8 answers

Setting HTTP headers

I'm trying to set a header in my Go web server. I'm using gorilla/mux and net/http packages. I'd like to set Access-Control-Allow-Origin: * to allow cross domain AJAX. Here's my Go code: func saveHandler(w http.ResponseWriter, r *http.Request) { //…
Zen
  • 7,197
  • 8
  • 35
  • 57
179
votes
8 answers

What's the meaning of interface{}?

I'm new to interfaces and trying to do SOAP request by github I don't understand the meaning of Msg interface{} in this code: type Envelope struct { Body `xml:"soap:"` } type Body struct { Msg interface{} } I've observed the same…
user
  • 2,694
  • 6
  • 24
  • 25