Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I can't seem to find any clear explanation of how to achieve this. I think my knowledge on type casting and conversion in Go isn't great.
Lets say I have the following slice:
myVector := []float64{0.1, 0.4444, 0.9999, 01}
For my particular use case I need to convert it into its []byte representation but can't seem to figure it out.
Any suggestions would be great. Thanks.
Generally you always have to iterate over a slice to convert it to another type of slice:
myVector := []float64{0.1, 0.4444, 0.9999, 01}
var newSlice []byte
for _, val := range myVektor {
newVal := convert(val)
newSlice = append(newSlice, newVal)
}
The convert function is up to you depending on what you expect a float64 to byte conversion to look like.
Note: In case newVal is not a single value, but multiple use newSlice = append(newSlice, newVal...) instead.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a raw string which is formed from array of strings like the one defined below
"['a','b','c']"
which i want to convert to proper array of string as below
["a","b","c"]
I am trying this on golang. However, i didn't succeed in getting this done. How this can be accomplished in go
var s = "['a','b','c']"
ss := strings.Split(strings.Trim(s, "[]"), ",")
a := make([]string, len(ss))
for i := range ss {
a[i] = strings.Trim(ss[i], "'")
}
out, err := json.Marshal(a)
if err != nil {
panic(err)
}
fmt.Println(string(out))
// output: ["a","b","c"]
https://go.dev/play/p/kQ0Up06K9zz
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
why this program is not throwing any null pointer exception
https://go.dev/play/p/37reql6Pdb5
eventhough m is null, still accessing on m.
Check out this code snippet:
package main
import "fmt"
func main() {
var m map[string]string = nil
fmt.Println(m) // output: "map[]"
}
This is an intended behavior since nil acts as zero value for many types of variables, including (but not limited to) maps and slices.
Also, there's no such thing in Golang as "Exception". You can check if a value exists in your map using this syntax:
value, found := m["key"]
where the found variable indicates if the value exists in the map.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I try to get myself familiarise with interface{} in Go. I tried:
var m map[string]string
m["time"] = "asdf"
and get error:
assignment to entry in nil map
I am not sure why I get the error.
When you declare a variable like this:
var m map[string]string
your m variable is assigned with a default value of map, which is nil, not an empty map. This is why you get that error, you are trying to add a value to a nil map.
To initialize an empty map, you can try any of these:
var m map[string]string = map[string]string{}
m := make(map[string]string)
m := map[string]string{}
Here is an article on default values for all Go types.
You need to use make(built-in function) like
make(map[string]string, 0)
to initialize the map
https://golang.org/doc/effective_go.html#allocation_make
https://golang.org/doc/effective_go.html#maps
The value of map m is nil.
The make function allocates and initializes a hash map data structure and returns a map value that points to it.
m := make(map[string]string)
m["time"] = "asdf"
Read allocations with make section of specs.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to find if a variable is of type float64:
package main
import ("fmt")
func main() {
myvar := 12.34
if myvar.(type) == float64 {
fmt.Println("Type is float64.")
}
}
However, it is not working and giving following error:
./rnFindType.go:6:10: use of .(type) outside type switch
./rnFindType.go:6:21: type float64 is not an expression
What is the problem and how can it be solved?
You know that myvar is a float64 because the variable is declared with the concrete type float64.
If myvar is an interface type, then you can use a type assertion to determine if the concrete value is some type.
var myvar interface{} = 12.34
if _, ok := myvar.(float64); ok {
fmt.Println("Type is float64.")
}
Try this program at https://play.golang.org/p/n5ftbp5V2Sx
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm thinking up about how I find the same characters in one variable looks like this:
var words string = "abab"
and then I want's to eliminate the same characters in that one variable and here's the output to be
Output:
ab
have any solution about this?
One solution can be the use of go map[] to track the taken characters.
sample code:
func main() {
s := "abcdaabcefgahccij"
newS := ""
taken := make(map[rune]int)
for _, value := range s {
if _, ok := taken[value]; !ok {
taken[value] = 1
newS += string(value)
}
}
fmt.Println(newS)
}
Output:
abcdefghij