Question

Include and Find Files Included in Go Function

I’ve searched through the documentation to the best of my ability, and can’t find anything that helps me understand how to simply include a 200kb .jpg file in with my function.

I’m creating a Go Function that needs to use a base image for creating a new modified image based on some parameters. I’d like to have it somewhere on disk that my function can simply use it load it to a buffer, modify the buffer, return the buffer as an image response.

Based on the docs, it seems everything around my go code should be zipped up and in the container. I’ve done some looking, and I can’t seem to find where my image is landing (or even if it’s being included at all).

I do not have an ignore file, nor an include. I’ve tried an include, but still could not find it during runtime.

My project is essentially:

Root
.Packages
..image-func
...code.go
...base.jpg

My temporary solution will be to download it at runtime, and attempt to cache it in /tmp to promote reuse; but this is annoying and I need to maintain hosting the image somewhere independently from my source.

Any information/more specifics on the go build would be greatly appreciated.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Matt Welke
DigitalOcean Employee
DigitalOcean Employee badge
May 24, 2023
Accepted Answer

For Go functions, the approach we recommend for including arbitrary files alongside deployed function code is to use Go’s embed package. You’re right that right now, our docs don’t cover this. Thanks for posting this to let us know. We’re going to update our documentation to include this.

In the mean time, here’s an example for your use case. It embeds a PNG image file. I took one of the images from one of our blog posts, sammy-jetpack.png.

My directory structure:

.
├── packages
│   └── go-embed-image-example
│       └── fn
│           ├── main.go
│           └── sammy-jetpack.png
└── project.yml

My project.yml file:

packages:
  - name: go-embed-image-example
    functions:
    - name: fn
      runtime: go:1.20

My main.go file:

package main

import (
    _ "embed"
    b64 "encoding/base64"
)

var (
    //go:embed sammy-jetpack.png
    img []byte
)

type ResponseHeaders struct {
    ContentType string `json:"Content-Type"`
}

type Response struct {
    Body string          `json:"body"`
    Headers ResponseHeaders `json:"headers"`
}

func Main() Response {
    return Response{
        Body: b64.StdEncoding.EncodeToString(img),
        Headers: ResponseHeaders{
            ContentType: "image/png",
        },
    }
}

My example returns the image as is. Loading its URL in a web browser displays the image as the response body. To help me create the structs used for the response, including its headers, I consulted our docs on returning images from functions.

You would expand the example to add the processing for your use case.

Bobby Iliev
Site Moderator
Site Moderator badge
May 24, 2023

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.