logo-MindMajix
PostGoLangGoLang
p
persis crystal

Posted on 26th July 2024|8621 views

0
votes

Golang Invalid Indirect

What does invalid indirect of a literal mean?

1 answers
Answers
P
naveen nani

Posted on 26th July 2024| views

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
Name
Course *
Email *
Phone Number