Browse code

client: fix example in README (align with ExampleNew())

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/11/13 01:18:09
Showing 3 changed files
... ...
@@ -23,19 +23,28 @@ import (
23 23
 )
24 24
 
25 25
 func main() {
26
+	// Create a new client that handles common environment variables
27
+	// for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does
28
+	// API-version negotiation to allow downgrading the API version
29
+	// when connecting with an older daemon version.
26 30
 	apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation())
27 31
 	if err != nil {
28 32
 		panic(err)
29 33
 	}
30 34
 	defer apiClient.Close()
31 35
 
32
-	containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true})
36
+	// List all containers (both stopped and running).
37
+	result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
38
+		All: true,
39
+	})
33 40
 	if err != nil {
34 41
 		panic(err)
35 42
 	}
36 43
 
37
-	for _, ctr := range containers {
38
-		fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status)
44
+	// Print each container's ID, status and the image it was created from.
45
+	fmt.Printf("%s  %-22s  %s\n", "ID", "STATUS", "IMAGE")
46
+	for _, ctr := range result.Items {
47
+		fmt.Printf("%s  %-22s  %s\n", ctr.ID, ctr.Status, ctr.Image)
39 48
 	}
40 49
 }
41 50
 ```
... ...
@@ -17,6 +17,7 @@ func ExampleNew() {
17 17
 	if err != nil {
18 18
 		log.Fatal(err)
19 19
 	}
20
+	defer apiClient.Close()
20 21
 
21 22
 	// List all containers (both stopped and running).
22 23
 	result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
... ...
@@ -23,19 +23,28 @@ import (
23 23
 )
24 24
 
25 25
 func main() {
26
+	// Create a new client that handles common environment variables
27
+	// for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does
28
+	// API-version negotiation to allow downgrading the API version
29
+	// when connecting with an older daemon version.
26 30
 	apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation())
27 31
 	if err != nil {
28 32
 		panic(err)
29 33
 	}
30 34
 	defer apiClient.Close()
31 35
 
32
-	containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true})
36
+	// List all containers (both stopped and running).
37
+	result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
38
+		All: true,
39
+	})
33 40
 	if err != nil {
34 41
 		panic(err)
35 42
 	}
36 43
 
37
-	for _, ctr := range containers {
38
-		fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status)
44
+	// Print each container's ID, status and the image it was created from.
45
+	fmt.Printf("%s  %-22s  %s\n", "ID", "STATUS", "IMAGE")
46
+	for _, ctr := range result.Items {
47
+		fmt.Printf("%s  %-22s  %s\n", ctr.ID, ctr.Status, ctr.Image)
39 48
 	}
40 49
 }
41 50
 ```