> ## Documentation Index
> Fetch the complete documentation index at: https://cloudinary.com/documentation/llms.txt
> Use this file to discover all available pages before exploring further.

# Upload images in Go (video tutorial)

[githublink]: https://github.com/cloudinary-community/cloudinary-examples/tree/main/examples/go-image-upload

## Overview

Learn how to upload images in Go using the Cloudinary Go SDK.

## Video tutorial

  This video is brought to you by Cloudinary's video player - embed your own!Use the controls to set the playback speed, navigate to chapters of interest and select subtitles in your preferred language.

> **TIP**: :title=View the code

You can find the code from this tutorial in [GitHub][githublink].
## Tutorial contents
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.### Introduction
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=00 :player=cld} | One of the most important parts of your workflow is getting your images and videos into the Cloudinary Media Library. In this tutorial, you'll learn how to use the Go SDK to upload one or multiple images to Cloudinary.

### Upload a single image from your local file system
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=17 :player=cld} | Once the Cloudinary Go module is installed, imported, and configured, you can upload local images from your directory. Start by creating a context with `context.Background()` and save it to the variable `ctx`. Then call the Cloudinary uploader method, passing the image path as an argument. Make sure to reference the correct path in your file system. Save the upload response in a variable and add proper error handling. Finally, print out the response as formatted JSON for easy reading.

```go
package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    
    "github.com/cloudinary/cloudinary-go/v2"
    "github.com/cloudinary/cloudinary-go/v2/api/uploader"
    "github.com/joho/godotenv"
)

func main() {
    // Load the environment variables from the .env file
    err := godotenv.Load()
    if err != nil {
        log.Fatalf("Error loading .env file: %v", err)
    }
    
    // Initialize Cloudinary with credentials from the environment variable CLOUDINARY_URL
    cld, err := cloudinary.New()
    if err != nil {
        log.Fatalf("Failed to initialize Cloudinary: %v", err)
    }
    // Create a context
    ctx := context.Background()
    
    // Upload the image
    resp, err := cld.Upload.Upload(ctx, "images/butterfly.jpeg", uploader.UploadParams{})
    if err != nil {
        log.Fatalf("Error uploading image: %v", err)
    }
    
    // Marshal the data into a pretty JSON string
    response, err := json.MarshalIndent(resp, "", "  ")
    if err != nil {
        fmt.Println("Failed to generate JSON", err)
        return
    }

    // Log the response
    fmt.Println("Upload response:")
    fmt.Println(string(response))
}
```

### Upload response
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=51 :player=cld} | When you run the script, it returns a comprehensive upload response containing metadata and the delivery URL for your image. You can then open the image in a browser window to verify it's delivered correctly from Cloudinary.

### Upload multiple images from your local file system
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=01 :player=cld} | To upload multiple files efficiently, first define their file paths in an array, then use a Go loop to iterate over this array. Inside the loop, place the Cloudinary `uploader` code and replace the static image path with the loop variable that represents the current image path. Be sure to add proper error handling to catch any issues during the upload process. This approach ensures each image in the array gets uploaded successfully. For cleaner output, instead of printing the entire upload response for each image, you can log only the secure URLs.

```go
// Array of image paths
imagePaths := []string{
        "images/butterfly.jpg",
        "images/landscape.jpg",
        "images/portrait.jpg",
}
    
// Upload the images
for _, path := range imagePaths {
    result, err := cld.Upload.Upload(ctx, path, uploader.UploadParams{})
    if err != nil {
        log.Printf("Error uploading image %s: %v", path, err)
        continue
    }
        
// Log the delivery URLs
fmt.Printf("Path: $x\nDelivery Url: %s\n\n", path, resp.SecureURL)
}
```

### Upload a single image from a remote URL
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=41 :player=cld} | While we've covered uploading images from your local directory, Cloudinary's uploader is versatile and accepts images from other sources as well. You can upload images directly from remote URLs, allowing you to pull images from other servers directly into your Cloudinary account. The process is straightforward: simply replace the local image path with a web image URL in your uploader call. When you run the code, it returns the Cloudinary delivery URL for the uploaded image, which you can then open in a browser to verify the successful upload.

```go
// Upload from remote URL
resp, err := cld.Upload.Upload(ctx, "https://raw.githubusercontent.com/cloudinary-devs/cld-docs-assets/refs/heads/main/assets/images/models.jpg", uploader.UploadParams{})
if err != nil {
    log.Printf("Error uploading image:", err)
    return
}
// Log the delivery URL
fmt.Printf("Delivery URL: %s\n\n", resp.SecureURL)
```

## Keep learning

> **READING**:
>
> * Learn more about [uploading images and videos using the Go SDK](go_image_and_video_upload).

> * Take a look at our [Upload guide](upload_images) to learn about uploading to Cloudinary in general.

> * Use the [Upload API reference](image_upload_api_reference) to find all the options and parameters available.

> * Watch more [Dev Hints videos](https://www.youtube.com/playlist?list=PL8dVGjLA2oMpaTbvoKCaRNBMQzBUIv7N8) on the [Cloudinary YouTube channel](https://www.youtube.com/cloudinary).

#### If you like this, you might also like...

  
  
  
    Configure the Go SDK
    Install and configure the Cloudinary Go SDK 
  

  
  
  
    Upload Programmatically
    Use a Cloudinary SDK to upload media assets 
  

  
  
  
    Use a Webhook to Remove Backgrounds on Upload
    Use a webhook notification to remove image backgrounds on upload 
  

&nbsp;

&nbsp;Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.
&nbsp;