This question already has answers here:
Get value from pointer
(1 answer)
How do I print the pointer value of a Go object? What does the pointer value mean?
(5 answers)
get the value from a pointer to a float32 from inside a struct?
(1 answer)
How to convert a 'string pointer' to a string in Golang?
(3 answers)
What does asterisk (*struct) notation mean in golang
(2 answers)
Closed 14 days ago.
When I retrieve data with the type of *bool(Pointer bool?) in Go I get the hex value "0xc0001ed477".
But how can I transform this to false or true?
I saw some examples from bool to *bool but not a reverse solution.
Is there any documentation about this?
You simply dereference it:
var boolPtr *bool = new(bool)
var b = *boolPtr
fmt.Println(b) // false
See "Address operators" at https://go.dev/ref/spec#Operators.
For an operand x of pointer type *T, the pointer indirection *x denotes the variable of type T pointed to by x.
Related
This question already has answers here:
How can you get a reflect.Type instance of an struct without physically creating the struct?
(1 answer)
TypeOf without an instance and passing result to a func
(2 answers)
How to get Type representation from name via reflection?
(1 answer)
Closed 8 months ago.
I have this Go code:
type Op interface{}
type Op1 struct{}
type Op2 struct{}
I'm trying to map Types to int. Something like this (pseudo-code):
mapping := map[reflect.Type]int
mapping[Op1] = 20
mapping[Op2] = 30
I know this is possible by creating an instance of Op1/Op2 and call reflect.TypeOf:
op1 := Op1{}
op2 := Op1{}
mapping := map[reflect.Type]int
mapping[reflect.TypeOf(op1)] = 20
mapping[reflect.TypeOf(op2) = 30
but my question is if it's possible to do that "statically" without creating an object. Something like .class in Java.
This question already has answers here:
Are pointers dereferenced by default inside of methods?
(2 answers)
Closed 8 months ago.
Consider the below example
type Employee struct {
Firstname string
// other fields
}
func (e *Employee) SetName(name string) {
e.Firstname = name // type 1
(*e).firstName = name // type 2
}
What is the difference between type 1 and type 2 ways of accessing properties here? When should we use one over the other?
Type 1 is a shorthand for type 2. Use the shorthand notation.
Here's the quote from the specification:
if the type of x is a defined pointer type and (*x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (*x).f.
This question already has answers here:
Convert []string to []interface{} [duplicate]
(3 answers)
Converting slice of structs to slice of empty interface [duplicate]
(1 answer)
Why can't I substitute a slice of one type for another in Go?
(3 answers)
Why can't I pass a `func() []int` as `func() []interface{}` in go?
(2 answers)
Why a slice []struct doesn't behave same as []builtin?
(3 answers)
Closed 4 years ago.
I'm trying to write a function that can take any type (more specifically, trying to get it to take any type of protobuffer, but I'll settle for being more broadly generic if I have to). Based on what I've read, it seems like it can be done like this:
func processSlice(mySlice []interface{}) void{
// Do stuff in here
}
When I try to use this with a slice of protos, though, I get the following error:
cannot use myProtoSlice (type []*MyProto) as type []interface{} in argument to processSlice
You can't cast slices from a slice of one type to another (as you can types), it would be expensive, and they decided it was better to force you to be explicit.
It would probably be less painful to simply write the function for each slice type you actually need to handle (if you know the types and there are not many).
As the error clearly depicts:
cannot use myProtoSlice (type []*MyProto) as type []interface{} in
argument to processSlice
[]interface{} of interface is not interface{} type it is different. Golang is strict about types.
Change the slice of interface to just interface to wrap the value you are receiving in your function. Modify below code:
func processSlice(mySlice []interface{}) void{
// Do stuff in here
}
To passing an interface
func processSlice(mySlice interface{}) void{
// Do stuff in here
}
This question already has answers here:
Golang: I have a map of int to struct. Why can't I directly modify a field in a map value? [duplicate]
(1 answer)
Why do I get a "cannot assign" error when setting value to a struct as a value in a map? [duplicate]
(2 answers)
Cannot assign to struct field in a map
(5 answers)
Closed 4 years ago.
I have the next structure:
type filesDB struct {
Name string
Content []byte
}
type fileDB struct {
Files map[string]filesDB
}
Well, when I try to save data in JSON format with the maps, I try this:
First, I make a map var:
var filesDatabase = map[string]fileDB{}
Then, i try to insert data:
var f filesDB
fidb := map[string]filesDB{}
f.Name= filename
f.Content= encrypt(b, sUser[user].Cipher)
fidb[filename] = f
filesDatabase[user].Files = fidb
jsonDB, err := json.Marshal(filesDatabase)
So, when I try to run the script, I get the next error:
cannot assign to struct field filesDatabase[user].Files in map
How can I resolve this error? Thanks!
This question already has answers here:
why is indexing on the slice pointer not allowed in golang
(2 answers)
Closed 4 months ago.
Is there a short hand for accessing x.Bar[0] in the following case?
The direct attempt results in (type *[]string does not support indexing) for obvious reasons
type A struct {
B *[]string
}
x := &Foo{Bar: &[]string{"1", "2"}}
It would be
(*x.Bar)[0]
You use parentheses to change the precedence of operators: [] has a higher precedence than *.