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
1323
votes
11 answers

How to check if a map contains a key in Go?

I know I can iterate over a map m with for k, v := range m { ... } and look for a key, but is there a more efficient way of testing for a key's existence in a map?
grokus
  • 18,046
  • 9
  • 29
  • 35
956
votes
19 answers

How to efficiently concatenate strings in go

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string. So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
884
votes
12 answers

How do you write multiline strings in Go?

Does Go have anything similar to Python's multiline strings: """line 1 line 2 line 3""" If not, what is the preferred way of writing strings spanning multiple lines?
aeter
  • 11,960
  • 6
  • 27
  • 29
803
votes
9 answers

Concatenate two slices in Go

I'm trying to combine the slice [1, 2] and the slice [3, 4]. How can I do this in Go? I tried: append([]int{1,2}, []int{3,4}) but got: cannot use []int literal (type []int) as type int in append However, the documentation seems to indicate this is…
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
763
votes
9 answers

Is there a foreach loop in Go?

Is there a foreach construct in the Go language? Can I iterate over a slice or array using a for?
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
754
votes
14 answers

What is an idiomatic way of representing enums in Go?

I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G}. I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in…
carbocation
  • 8,806
  • 7
  • 25
  • 30
753
votes
10 answers

How to convert an int value to string in Go?

i := 123 s := string(i) s is 'E', but what I want is "123" Please tell me how can I get "123". And in Java, I can do in this way: String s = "ab" + "c" // s is "abc" how can I concat two strings in Go?
hardPass
  • 19,033
  • 19
  • 40
  • 42
744
votes
15 answers

Optional Parameters in Go?

Can Go have optional parameters? Or can I just define two different functions with the same name and a different number of arguments?
devyn
  • 16,615
  • 6
  • 24
  • 14
697
votes
31 answers

How to print struct variables in console?

How can I print (to the console) the Id, Title, Name, etc. of this struct in Golang? type Project struct { Id int64 `json:"project_id"` Title string `json:"title"` Name string `json:"name"` Data Data …
fnr
  • 9,007
  • 5
  • 17
  • 16
658
votes
14 answers

How to check if a file exists in Go?

Go's standard library does not have a function solely intended to check if a file exists or not (like Python's os.path.exists). What is the idiomatic way to do it?
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
588
votes
4 answers

What are the use(s) for struct tags in Go?

In the Go Language Specification, it mentions a brief overview of tags: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. The tags are…
liamzebedee
  • 14,010
  • 21
  • 72
  • 118
559
votes
15 answers

What is the idiomatic Go equivalent of C's ternary operator?

In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -val Go doesn't have the conditional operator. What is…
Fabien
  • 12,486
  • 9
  • 44
  • 62
557
votes
16 answers

How to find the type of an object in Go?

How do I find the type of an object in Go? In Python, I just use typeof to fetch the type of object. Similarly in Go, is there a way to implement the same ? Here is the container from which I am iterating: for e := dlist.Front(); e != nil; e =…
Rahul
  • 11,129
  • 17
  • 63
  • 76
551
votes
8 answers

Format a Go string without printing?

Is there a simple way to format a string in Go without printing the string? I can do: bar := "bar" fmt.Printf("foo: %s", bar) But I want the formatted string returned rather than printed so I can manipulate it further. I could also do something…
Carnegie
  • 5,645
  • 2
  • 15
  • 7
548
votes
11 answers

How can I convert a zero-terminated byte array to string?

I need to read [100]byte to transfer a bunch of string data. Because not all of the strings are precisely 100 characters long, the remaining part of the byte array is padded with 0s. If I convert [100]byte to string by: string(byteArray[:]), the…
Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73
1
2 3
99 100