#Nil struct isn't report as null in "a == nil"

30 messages · Page 1 of 1 (latest)

wanton pelican
#

Hello guys.
It might be a dumb problem but I can't figure out. I've a structure called a. This structure looks like to be nil but the check a == nil returns always false. It comes weird when I print the variable and it show "nil".
Since I've trust issues, I ran the debugger and it shows that:
It indicates the variable has a type but indicates (nil) so I don't know what to think and how to solve this.
Thanks for your help

#

Here is the logs:

log.Printf("??? !! %v == %v", a, a == nil)

Gives: ??? !! <nil> == false

whole yarrow
#

however here your interface is not nil, it is pointing to a nil value

#

interfaces have two values, a type pointer and a data pointer, the type pointer points to the runtime type *generator.member and the data value point to the data

#

here your type pointer is not nil, but the data pointer is nil, and a == nil only checks the type pointer

#
var a any = (*generator.member)(nil)
fmt.Println(a == nil) // false

you are in this situation above

#

you have a type within that interface so the interface is not nil, but the value attached is nil

#

== nil on an interface only check the interface not the underlying value

#

an other way to look at it is that == nil on an interface cannot check for the underlying value, because what if I try to compare something that is not nil ? For example what if I have an uint in my interface, how would == nil check against the uint ?

wanton pelican
#

Great explanations, thanks. What if I want to know if the underlying value is null ? I know what the underlying type is when I want to check the value.

whole yarrow
#

else you can either use type assertions or methods or reflect (neither solution is good, imo methods is the least worst one)

wanton pelican
#

What is the "methods" way to do it ?

#

I'm seeking for the more optimized way since this part of code is called a lot of times.

whole yarrow
wanton pelican
#

Yes?

whole yarrow
#

Sorry but why ? You are writing a json parser ?

wanton pelican
#

I mean, performance is not the key but it definitely need to be taken in consideration

#

A savegame editor for a game. It reads the XML files and generate Go files from it

whole yarrow
#

it's not like one solution is O(n), you are looking at a delta of 2ns at most

wanton pelican
#

I'm completely open to suggestion. I'm far away to be an expert

whole yarrow
#

so usually when you generate code you run the codegen once, and then you just use the already generated code in the application

wanton pelican
#

Yea tha's right

whole yarrow
#

else you need to include a compiler in the application

wanton pelican
whole yarrow
#

I would refactor my code to rely on checking nils, if you don't want to do this I would do this:

type I interface{
  IsEmpty() bool
}

type A struct{}

func (a *A) IsEmpty() bool {
  return a == nil
}

// ...
func checkInterfaceEmpty(i I) bool {
  return i == nil || i.IsEmpty()
}
#

it's not the fastest but should be fine

wanton pelican