Post

GoLangGoLang

p
persis crystal

Posted on 8th May 2024|8543 views

0
votes

Golang Invalid Indirect

What does invalid indirect of a literal mean?

Answers
P
naveen nani

Posted on 8th May 2024

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))

}

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos