Getting starting with client/api Incus Golang API

Thanks for all the great work here folks! Just wanted to share two pain points I had when writing my first Go program to start / exec an interactive terminal following the API docs examples: incus package - github.com/lxc/incus/client - Go Packages

  • It was not clear what to actually import to get access to incus.<...> and api.<...>
  • I was unable to create a container without modifying the default example

Here’s the main parts of the code in case it helps others:

import (
    incus "github.com/lxc/incus/client"
    "github.com/lxc/incus/shared/api"
)

// ...

req := api.InstancesPost{
		Name: "first",
		Type: "container",
		Source: api.InstanceSource{
			Type:     "image",
			Alias:    "alpine/3.20",
			Server:   "https://images.linuxcontainers.org",
			Protocol: "simplestreams",
		},
	}

Where Server / Protocol are required.

Also the example references a name variable further down which is not actually defined. Instead plain strings are used for the names. If there is interest in updating these docs, I can send a PR!

PRs are always welcome, especially to improve the Go API client docs as that’s something that I don’t believe anyone has really touched since I wrote those years ago for LXD :slight_smile:

We do have the tendency, at least internally to just import “github.com/lxc/incus/client” without the explicit name tag (“incus”) as we know it’s going to provide the “incus” package.

This is something that should probably be mentioned in the docs as even if the example does show the “incus” name for that import, anyone looking at any of our other Go packages as source of example may get confused.

1 Like