I'm getting this error (below) [root@localhost test]# ./test virError(Code=6, Domain=20, Message='invalid connection pointer in virConnectListAllDomains') 0 running domains: when running this program :- (below) package main import ( "fmt" libvirt "github.com/libvirt/libvirt-go" ) type DomService struct { Conn *libvirt.Connect } func (d *DomService) Connect() error { var err error d.Conn, err = libvirt.NewConnect("qemu:///system") if err != nil { fmt.Println(err) } defer d.Conn.Close() return nil } func (d *DomService) ListDoms() error { doms, err :d.Conn.ListAllDomains(libvirt.CONNECT_LIST_DOMAINS_ACTIVE) if err != nil { fmt.Println(err) } //fmt.Println(doms) fmt.Printf("%d running domains:\n", len(doms)) for _, dom := range doms { name, err := dom.GetName() if err == nil { fmt.Printf(" %s\n", name) } dom.Free() } return nil } func main() { doms := DomService{} doms.Connect() doms.ListDoms() } -- Regards, Shashwat Shagun
Please consider writing more descriptive subjects. No need to shout, too. On Fri, Feb 15, 2019 at 08:43:36PM +0530, Shashwat shagun wrote: [...]>func (d *DomService) Connect() error { > var err error > d.Conn, err = libvirt.NewConnect("qemu:///system") > if err != nil { > fmt.Println(err) > } >defer d.Conn.Close()I have no idea how the golang bindings are structured, but I'm quite sure you don't want to close the connection right after this function ends...
On Fri, Feb 15, 2019 at 04:20:34PM +0100, Martin Kletzander wrote:> Please consider writing more descriptive subjects. No need to shout, too. > > On Fri, Feb 15, 2019 at 08:43:36PM +0530, Shashwat shagun wrote: > > [...] > > > func (d *DomService) Connect() error { > > var err error > > d.Conn, err = libvirt.NewConnect("qemu:///system") > > if err != nil { > > fmt.Println(err) > > } > > defer d.Conn.Close() > > I have no idea how the golang bindings are structured, but I'm quite sure you > don't want to close the connection right after this function ends...Yes, this code is just causing the connection to close immediately when Connect() returns, so the code to list domains won't ever get an open connection. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|