Skip to main content
1438 votes
12 answers
1.6m views

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's user avatar
  • 19.4k
1020 votes
20 answers
801k views

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 ...
Randy Sugianto 'Yuku''s user avatar
959 votes
13 answers
579k views

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's user avatar
  • 12.9k
861 votes
11 answers
714k views

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 ...
Kevin Burke's user avatar
  • 65.8k
844 votes
11 answers
883k views

i := 123 s := string(i) s is 'E', but what I want is "123" Please tell me how can I get "123".
hardPass's user avatar
  • 21.2k
825 votes
15 answers
702k views

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 ...
carbocation's user avatar
  • 9,668
817 votes
15 answers
608k views

Can Go have optional parameters? Or can I just define two different functions with the same name and a different number of arguments?
devyn's user avatar
  • 17.6k
810 votes
9 answers
693k views

Is there a foreach construct in the Go language? Can I iterate over a slice or array using a for?
tatsuhirosatou's user avatar
759 votes
19 answers
833k views

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"`...
fnr's user avatar
  • 9,807
727 votes
15 answers
570k views

Does the Go standard library have a function which checks if a file exists (like Python's os.path.exists). Is there an idiomatic way to check file existence / non-existence?
Sridhar Ratnakumar's user avatar
624 votes
14 answers
576k views

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 ...
Fabien's user avatar
  • 13.6k
619 votes
4 answers
194k views

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 ...
liamzebedee's user avatar
  • 14.6k
600 votes
16 answers
707k views

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 ...
Rahul's user avatar
  • 11.8k
590 votes
13 answers
665k views

I'm unable to find file.ReadLine function in Go. How does one read a file line by line?
g06lin's user avatar
  • 6,225
587 votes
8 answers
461k views

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 ...
Carnegie's user avatar
  • 6,005

15 30 50 per page
1
2 3 4 5
4967