go lang map not throwing null pointer [closed] - go

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.

Related

get pointer to variable stored in reflect.Value in Golang [closed]

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 28 days ago.
Improve this question
Probably this is question is very simple, but i've spent some time and was not able to find any good solution(
For example we have the following code:
var someValue int = 10
v := reflect.ValueOf(someValue)
var v1 reflect.Value // should refer to &someValue, but with using of the "v" variable only
I know that the easiest solution will be v1 := reflect.ValueOf(&someValue) or using of reflect.New() function, but unfortunately this will not work in my case.
The "v1" variable should be initialized only with using of the "v" variable.
When you call reflect.ValueOf(someValue), ValueOf is passed a copy of someValue. The address of the argument to ValueOf is different from the address of someValue at the call site.
Whatever ValueOf returns cannot possibly know about the address of the original someValue, so what you want to achieve here is impossible.
The best you can do is call Value.Addr, which will return a reflect.Value representing the address of the copy of someValue that was passed to ValueOf:
var someValue int = 10
v := reflect.ValueOf(someValue)
v1 := v.Addr() // reflect.Value representing a *int pointing to a copy of someValue
var p1 *int = v1.Interface().(*int) // distinct from &someValue

"assignment to entry in nil map" with simple interface assignment not working [closed]

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.

How to convert a []float64 to []byte? [closed]

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.

Go newbie: Getting "mismatched types *int and int" error when trying to compare reference to static integer [closed]

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'm learning Go and trying to use reference to integer value in if-clause.
package main
import (
"fmt"
)
func main() {
a := 19
b := &a
if b > 10 {
fmt.Println("Its bigger")
}
}
This gives error message for type mismath.
How could I successfully compare value which b is referencing. In my training code I'm reading command line arguments with flags, but I suppose this example is reprex.
How should I compare when havin only reference available?
Here b is a pointer of int means *int. You can't compare *int type with int type.
Use *b to dereference to get the value and then compare with constant value.
if *b > 10 {
fmt.Println("Its bigger")
}

How to find if type is float64 [closed]

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

Resources