Creating Images with Go and Gemini
Using the Wrapper
With the generateGeminiImages() function at our disposal, we can now explore how to effectively utilize it for image creation. This lesson will guide you through the process of calling the function, saving the generated images, requesting multiple variations with repeated calls, and customizing the output by adjusting options such as aspect ratio. By the end of this lesson, you'll be equipped with the knowledge to generate and manage images using Go and Gemini 3.1 Flash.
Calling the helper
This Go code snippet demonstrates how to generate and save images using the generateGeminiImages function. The main function begins by defining a prompt string that describes the desired image content. It then calls generateGeminiImages with the prompt and no additional options, capturing the resulting image data in imageDataList. If an error occurs during image generation, it logs a warning message.
The code checks if any images were generated by evaluating the length of imageDataList. If images are present, it prints the number of generated images. It then specifies an output directory and constructs the file path for saving the first image. The image is saved using os.WriteFile, and if successful, a confirmation message is printed. If no images are generated, the program logs a fatal error and terminates.
This code snippet decodes the image from the response and saves it to a file.

Requesting multiple files
Gemini 3.1 Flash returns image parts from a single generateContent request, but this project does not send the older SampleCount field to the API. To create multiple variations, call generateGeminiImages multiple times and collect the returned image bytes from each request.
