Questions tagged [go]

Go is an open-source programming language. It is statically-typed, with a syntax loosely derived from C, adding automatic memory management, type safety, some dynamic typing capabilities, additional built-in types such as variable-length arrays (called slices) and key-value maps, and a large standard library.

Filter by
Sorted by
Tagged with
1113votes
11answers
948kviews

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

I know I can iterate over a map m by, for k, v := range m { ... } and look for a key but is there a more efficient way of testing a key's existence in a map? I couldn't find the answer in the ...
user avatar
  • 15.5k
867votes
19answers
567kviews

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 ...
user avatar
762votes
11answers
339kviews

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?
user avatar
  • 10.6k
687votes
8answers
439kviews

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 ...
user avatar
  • 54.5k
678votes
8answers
481kviews

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?
user avatar
660votes
12answers
432kviews

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 ...
user avatar
  • 7,761
650votes
10answers
536kviews

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 ...
user avatar
  • 16.3k
628votes
15answers
368kviews

Optional Parameters in Go?

Can Go have optional parameters? Or can I just define two functions with the same name and a different number of arguments?
user avatar
  • 14.9k
570votes
26answers
497kviews

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"`...
user avatar
  • 7,395
565votes
13answers
365kviews

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?
user avatar
538votes
11answers
503kviews

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]...
user avatar
520votes
3answers
125kviews

What are the use(s) for 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 ...
user avatar
490votes
16answers
488kviews

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 ...
user avatar
  • 10.2k
484votes
9answers
504kviews

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?
user avatar
  • 4,957
482votes
9answers
296kviews

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 ...
user avatar
  • 4,955

15 30 50 per page
1
2 3 4 5
4023