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? 12 Answers 12 Answer recommended by Go Language
I’m unable to find file.ReadLine function in Go. How does one read a file line by line? 12 Answers 12 Answer recommended by Go Language
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 the most idiomatic way to implement the same piece of … Read more
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 like: s := “foo: ” + bar But this becomes … Read more
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? 9 s 9
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 = e.Next() { … Read more
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 made visible through a reflection interface but are otherwise ignored. // A struct corresponding to … Read more
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 tailing 0s are displayed as ^@^@s. In C, the string will terminate … Read more
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? 13 s 13
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 `json:”data”` Commits Commits `json:”commits”` } 26 s 26 recommended by Go Language
Can Go have optional parameters? Or can I just define two functions with the same name and a different number of arguments? 15 s 15 recommended by Go Language