73,884
questions
1398
votes
11
answers
1.5m
views
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?
996
votes
20
answers
739k
views
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 ...
932
votes
13
answers
527k
views
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?
832
votes
10
answers
663k
views
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
...
813
votes
10
answers
817k
views
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 ...
804
votes
14
answers
641k
views
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 ...
790
votes
15
answers
565k
views
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?
790
votes
9
answers
640k
views
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?
739
votes
18
answers
788k
views
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"`...
702
votes
15
answers
527k
views
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?
607
votes
4
answers
182k
views
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 ...
594
votes
14
answers
516k
views
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
...
588
votes
16
answers
662k
views
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 ...
571
votes
8
answers
426k
views
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 ...
570
votes
13
answers
619k
views
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?