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.

func main() {
    prompt := "A serene sunset over a mountain range"
    
    imageDataList, err := generateGeminiImages(prompt, nil)
    if err != nil {
        log.Printf("Warning: %v", err)
    }
    
    if len(imageDataList) > 0 {
        fmt.Printf("✅ Generated %d images\n", len(imageDataList))
        
        outputDir := "server/static/images/"
        
        outputPath := filepath.Join(outputDir, "output-image.jpg")
        if err := os.WriteFile(outputPath, imageDataList[0], 0644); err != nil {
            log.Fatalf("❌ Failed to save image: %v", err)
        }
        
        fmt.Printf("✅ First image saved as %s\n", outputPath)
    } else {
        log.Fatal("❌ No images were generated")
    }
}

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. A serene sunset over a mountain range

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.

var imageDataList [][]byte
for i := 0; i < 2; i++ {
    images, err := generateGeminiImages(prompt, nil)
    if err != nil {
        log.Printf("Warning: %v", err)
        continue
    }

    imageDataList = append(imageDataList, images...)
}
Defining the Aspect Ratio

The AspectRatio option in the ImageGenerationParameters struct allows you to define the dimensions of the generated images in terms of width to height ratio. By setting AspectRatio: RATIO_16_9, the generateGeminiImages function is instructed to produce images with a 16:9 aspect ratio, which is a common widescreen format. This option provides flexibility in customizing the output to fit specific display or aesthetic requirements.

imageDataList, err := generateGeminiImages(prompt, &ImageGenerationParameters {
    AspectRatio: RATIO_16_9,
})

Possible ratios supported by this model are:

  • "1:1"
  • "16:9"
  • "9:16"
  • "4:3"
  • "3:4"
Summary and Next Steps

In this lesson, we explored how to use the generateGeminiImages function to create images based on a given prompt. We covered how to handle the generated image data, save it to files, request multiple variations by making repeated calls, and customize the output using options like AspectRatio for defining the image dimensions. With these tools, you can effectively generate and manage images using Go and Gemini 3.1 Flash.

Next, we will delve into more advanced customization options and explore how to integrate these image generation capabilities into larger applications.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal