Posted on 23rd September 2023
The “invalid indirect of literal” indicates you decided to pointer-dereference any struct literal: *foo{ ... }. Discard the star, or substitute it by an ampersand if you required a pointer literal. Here is the example.
package main
import "fmt"
import "reflect"
func dereferenceIfPtr(value interface{}) interface{} {
if reflect.TypeOf(value).Kind() == reflect.Ptr {
return reflect.ValueOf(value).Elem().Interface()
} else {
return value
}
}
func main() {
fmt.Println("456" == dereferenceIfPtr("456"))
x := "456"
fmt.Println("456" == dereferenceIfPtr(x))
fmt.Println("456" == dereferenceIfPtr(&x))
}
STILL GOT QUERIES?
Copyright © 2013 - 2023 MindMajix Technologies