Note the error says that val was nil, so your type assertion will panic. You 
can read more about how to make type assertions that report success or failure 
in the Go Tour: https://tour.golang.org/methods/15

Have you tried printing the type of what you receive before attempting the type 
assertion? I suspect that the system you are working with is passing you a 
variety of different types depending on the context, so you'll need to be 
flexible in what you handle.





On Tue, 12 Jan 2021, at 5:55 AM, Steve Murphy wrote:
> Many thanks, Ian, I appreciate your suggestions!
> 
> I added your code snippet to my program, and got:
> 
> panic: interface conversion: interface {} is nil, not *interface {} 
> [recovered]
> panic: interface conversion: interface {} is nil, not *interface {}
> 
> According to the trace, it looks like it's crashing while trying to evaluate 
> the valDeref := line.
> 
> Looking at the trace provided:
> 
> genBox: arg type=*int64 argval=0xc00001a150,     val type=<nil>, val 
> val=<nil> offset=0
> genBox: box=<nil>
> --- FAIL: TestKd1 (0.09s)
> panic: interface conversion: interface {} is nil, not *interface {} 
> [recovered]
> panic: interface conversion: interface {} is nil, not *interface {}
> 
> goroutine 6 [running]:
> testing.tRunner.func1(0xc000020200)
> /usr/lib/go-1.13/src/testing/testing.go:874 +0x3a3
> panic(0x523360, 0xc0000bc330)
> /usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
> parsetree.com/kdtree.genBox(0x50eec0, 0xc00001a150, 0x0, 0x0, 0x0, 0x2)
> /home/murf/go/kdtree/kdtree1_test.go:43 +0x43e
> 
> It kinda looks like the %T and %v aren't working for val; but the stack trace 
> shows 0xc00001a150 in the second argument to genBox.
> 
> Uh, wait a minute! According to the stack, the first arg (called arg) is 
> 0x50eec0; but the debug output from genbox shows arg's val is 0xc00001a150,
> which is what the second arg should be. So it looks like the args have 
> shifted to the left one position! And no errors on compilation?
> 
> I don't know what to think.
> 
> BTW, I'm on ubuntu 20.04, and the version of go is 1.13.
> 
> murf
> 
> On Monday, January 11, 2021 at 10:45:29 AM UTC-7 Ian Davis wrote:
>> __
>> In genBox your code is saying that val contains a pointer to interface{}. In 
>> other words its an interface{} that contains a *interface{}. That is a weird 
>> but valid construct.
>> 
>> I suggest you dereference it and see what the contained interface holds. 
>> Something like:
>> 
>> valDeref := val.(*interface{})
>> 
>>  fmt.Printf("val contains=%T", valDeref)
>> 
>> You may find that valDeref contains the *int64 you are looking for.
>> 
>> As an aside, *interface{} is usually an indication that someone somewhere is 
>> passing the wrong value to the function since it almost never makes sense to 
>> pass a pointer to interface{}.
>> 
>> 
>> 
>> 
>> 
>> On Mon, 11 Jan 2021, at 4:03 PM, Steve Murphy wrote:
>>> Hello!
>>> 
>>> I keep getting:
>>> panic: interface conversion: interface {} is *interface {}, not *int64 
>>> [recovered]
>>> panic: interface conversion: interface {} is *interface {}, not *int64
>>> 
>>> I *suspect* what is happening is that the KDElem struct has an item field 
>>> whose 
>>> type is interface{}, so that you can store a pointer to some struct (of 
>>> your own making),
>>> or an index into an array of structs, or... *whatever*, but in my case, 
>>> it's an index into an array
>>> of objects. The itemfunc (or genBox in my test code) is supposed to set the 
>>> item field in 
>>> the KDElem struct to the proper index, as the struct has just been created, 
>>> and the job
>>> of the itemfunc is to make it point to the right object, and set the bounds 
>>> info in the new struct.
>>> 
>>> type KdElem struct {
>>>     item       interface{} // a ptr to a particular struct, or an index 
>>> into an array of objects, or....
>>>     ...
>>> }
>>> 
>>> And the func that calls the itemfunc (genBox) looks like this:
>>> 
>>> func loadItems(itemfunc func(arg interface{}, val interface{}, size *KdBox) 
>>> int, arg interface{}, extent KdBox, length *int64, mean *float64) *KdElem {
>>>      ...
>>>      newItem = new(KdElem)
>>>      if itemfunc(arg, &newItem.item, newItem.size) != 0 {
>>>      ...
>>> 
>>> 
>>> And, in this case the itemfunc declaration looks like this:
>>> 
>>> func genBox(arg interface{}, val interface{}, box *KdBox) int {
>>>      var offsetp = arg.(*int64)         // successful
>>>      var offset = *offsetp                 // successful
>>>      // fmt.Printf("genBox: offset=%v offsetp=%v\n", offset, offsetp)
>>>      fmt.Printf("genBox: arg type=%T argval=%v,     val type=%T, val 
>>> val=%v\n", arg, arg, val, val)
>>>      if offset < KDBoxes {
>>>           fmt.Printf("genBox: val=%v  *int64=%v\n", val, val)
>>>           var realval *int64 = val.(*int64)     //  <<-- This is line 43!  
>>> Compiles.... but... Crash!!!
>>>           *realval = offset + 1
>>> ...
>>> 
>>> Now, I get this from a go test:
>>> 
>>> KdBuild: arg type=*int64  argval=0xc0000d2078; 
>>> genBox: arg type=*int64 argval=0xc0000d2078,     val type=*interface {}, 
>>> val val=0xc0000ba640
>>> genBox: val=0xc0000ba640  *int64=0xc0000ba640
>>> --- FAIL: TestKd1 (0.08s)
>>> panic: interface conversion: interface {} is *interface {}, not *int64 
>>> [recovered]
>>> panic: interface conversion: interface {} is *interface {}, not *int64
>>> 
>>> goroutine 19 [running]:
>>> testing.tRunner.func1(0xc000108100)
>>> /usr/lib/go-1.13/src/testing/testing.go:874 +0x3a3
>>> panic(0x523360, 0xc0000c2360)
>>> /usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
>>> parsetree.com/kdtree.genBox(0x50eec0, 0xc0000d2078, 0x50ef40, 0xc0000ba640, 
>>> 0x0, 0x0)
>>> /home/murf/go/kdtree/kdtree1_test.go:43 +0x270
>>> parsetree.com/kdtree.loadItems(0x5570f8, 0x50eec0, 0xc0000d2078, 
>>> 0x7fffffffffffffff, 0x7fffffffffffffff, 0x8000000000000000, 
>>> 0x8000000000000000, 0xc00096ab38, 0xc00096ab30, 0x0)
>>> /home/murf/go/kdtree/kdtree.go:683 +0xec
>>> parsetree.com/kdtree.KdBuild(0x5570f8, 0x50eec0, 0xc0000d2078, 0x0)
>>> /home/murf/go/kdtree/kdtree.go:168 +0x17e
>>> parsetree.com/kdtree.TestKd1(0xc000108100)
>>> /home/murf/go/kdtree/kdtree1_test.go:72 +0x13a
>>> testing.tRunner(0xc000108100, 0x5570f0)
>>> /usr/lib/go-1.13/src/testing/testing.go:909 +0
>>> 
>>> How do I set realval to the (*int64) value that's in val?
>>> 
>>> murf
>>> 
>>> 
>>> -- 
>>> 
>>> Steve Murphy
>>> ParseTree Corporation
>>> 57 Lane 17
>>> Cody, WY 82414
>>> ✉  murf at parsetree dot com
>>> ☎ 307-899-0510 <tel:(307)%20899-0510>
>>> 

>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to golang-nuts...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/golang-nuts/CAPPCp8HGQAcdw7J_vFLW8NKsXqfh5kgUtCrQn41dBLRamQggNA%40mail.gmail.com
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/CAPPCp8HGQAcdw7J_vFLW8NKsXqfh5kgUtCrQn41dBLRamQggNA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>> 
> 

> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/3763f6a4-0475-4c5a-96d5-33baea5348aan%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/3763f6a4-0475-4c5a-96d5-33baea5348aan%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/86cef77e-43fc-4177-ae82-e9152916898e%40www.fastmail.com.

Reply via email to