I'm trying to make a function that will look through a list (type container/list) of elevator-structs and return a pointer to the elevator that has the correct ip-address (the program is meant to control elevators on multiple computers) or nil if there is no elevator with this address.
Here is the function code:
func (e *ElevatorList)IPIsInList(ip string) *elevator{
for c := e.Elevators.Front(); c != nil; c = c.Next(){
if(c.Value.(elevator).Ip == ip){
return c.Value.(*elevator)
}
}
return nil
}
I think the casting in the first return line looks kind of fishy but it's one of the few implementations that hasn't caused compiler errors. When I run the program I first get the output from other functions, and when the IPIsInList() function is called i get the following:
panic: interface conversion: interface is **main.elevator, not main.elevator
runtime.panic+0xac /home/fredrik/go/src/pkg/runtime/proc.c:1254
runtime.panic(0x4937a8, 0xf84002aaf0)
assertE2Tret+0x11d /home/fredrik/go/src/pkg/runtime/iface.c:312
assertE2Tret(0x4927f8, 0x45e6d8, 0xf8400004f0, 0x7fbae00c1ed8, 0x10, ...)
runtime.assertE2T+0x50 /home/fredrik/go/src/pkg/runtime/iface.c:292
runtime.assertE2T(0x4927f8, 0x45e6d8, 0xf8400004f0, 0x28)
main.(*ElevatorList).IPIsInList+0x5b /home/fredrik/Dropbox/Programmering/go/listtest/elevatorList.go:72
main.(*ElevatorList).IPIsInList(0xf8400001c8, 0x4a806c, 0x2e3332310000000f, 0x0, 0x0, ...)
main.main+0x1f3 /home/fredrik/Dropbox/Programmering/go/listtest/main.go:53
main.main()
runtime.mainstart+0xf /home/fredrik/go/src/pkg/runtime/amd64/asm.s:78
runtime.mainstart()
runtime.goexit /home/fredrik/go/src/pkg/runtime/proc.c:246
runtime.goexit()
----- goroutine created by -----
_rt0_amd64+0xc9 /home/fredrik/go/src/pkg/runtime/amd64/asm.s:65
How should this be done? I have rewritten the function many times and I think it's the use of c.Value.(elevator) and/or c.Value.(*elevator) that's causing the problem. Here are the structs for elevator and Elevator_list:
type elevator struct {
Ip string
OrderList [FLOORS][3]int32
Floor int32
Dir int
Ms_since_ping int32
}
type ElevatorList struct {
Elevators *list.List
}
The elevators are added to the list with the function
func (e *ElevatorList) AddToList(newElevator *elevator){
e.Elevators.PushBack(&newElevator)
}