Accessing slice by index when slice is pointer [duplicate] - go

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 *.

Related

Convert *bool to bool in go [duplicate]

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.

Go: How to define a linked list struct that can use different types [duplicate]

This question already has answers here:
Implement generic linked list in golang which disallows different types in the same linkedlist
(2 answers)
Any type and implementing generic list in go programming language
(2 answers)
Generic Structs with Go
(1 answer)
Closed last month.
The usual linked list structure is as follows:
type IntNode struct {
Value int
Next *IntNode
Pre *IntNode
}
but when i want value to be string, i need another struct:
type StringNode struct {
Value string
Next *StringNode
Pre *StringNode
}
I don't want to use interface{} to reduce the consumption of assertions. Is there a feasible way to write different types through a structure?
Go 1.18 supports generics:
type LLNode[T any] struct {
Value T
Next *LLNode[T]
Prev *LLNode[T]
}
So now you can use:
intList := &LLNode[int]{Value: 1}
stringList := &LLNode[string]{Value: "abcd"}

How do I define a Go type constraint that's either a map or a slice? [duplicate]

This question already has answers here:
How to constrain type to types with index?
(1 answer)
How to iterate over a union of slices passed in a generic function? (T has no core type)
(2 answers)
Closed 9 months ago.
I'm trying to define a type constraint where it accepts either a []T or map[any]T, i.e. either a slice or a map with the same element value. The use case for this is specifically implementing a First function, that returns the first element from a collection.
This is my attempt, which is not working:
type collection[Value any] interface {
[]Value | map[any]Value
}
func first[Value any, Coll collection[Value]](coll Coll) (Value, bool) {
for _, value := range coll {
return value, true
}
var empty Value
return empty, false
}
Is this even possible? I might be running into limitations of what type constraints can, at the current time (Go 1.18).
A Go playground for this can be found here.

What does "i.(string)" actually mean in golang syntax? [duplicate]

This question already has answers here:
Is this casting in golang?
(1 answer)
What is the meaning of "dot parenthesis" syntax? [duplicate]
(1 answer)
Closed 4 years ago.
I recently started looking for functional go examples and I found this function:
mapper := func (i interface{}) interface{} {
return strings.ToUpper(i.(string))
}
Map(mapper, New(“milu”, “rantanplan”))
//[“MILU”, “RANTANPLAN”]
Now in this function, as you can see the return value of mapper is:
strings.ToUpper(i.(string)).
But, what does this i.(string) syntax mean? I tried searching, but didn't find anything particularly useful.
i.(string) casts (or attempts at least) i (type interface{}) to type string. I say attempts because say i is an int instead, this will panic. If that doesn't sound great to you, then you could change the syntax to
x, ok := i.(string)
In this case if i is not a string, then ok will be false and the code won't panic.
i.(string) means converting i(interface{} type) to string type.

"Cannot use myType as type interface{}"? I thought all types counted as interface{} in Go? [duplicate]

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
}

Resources