How to upload  files in Go step by step

How to upload files in Go step by step

When making a restful api uploading files is one of the key part.With go file upload is very easy to setup.This files may be images pdf or just some form data.In this post i am going to show you how easy it is to upload a file using go.

We are going to write a small program that  we will use to change a user profile image.

Project  Structure

Lets start with how our project structure will look like.

Note:i only show the important files.files that we use to save user on the database are not shown.

The handlers.go- has all the handlers we want to hook to our routes

The helpers-go-This contains all shared functions and resources.For example instead of writing the code to save a file multiple times we will write it once here.We shall later call this function every time we want to upload an image or file.

The Main.go – This is our entry point where we shall serve our application from.

 Writing The Code to handle file upload

We are going to  write the code to retrieve and save our file from the request object.

The function takes the request object as a parameter and  return an filename and error(if any occurs).

As you know we are going to be sending our files as form data(multipart/form-data) thus we need a way to retrieve  the file  from our request object.

As you can see this function has two return types

  1. file_name-this is what you store to the database to use in retrieving the file later.
  2. error-when calling this function you check error and it nil

 Making Handlers to handle our form request

Now that we have our function to save our file and image upload in golang ready, its time to start using it.We are going to use the httprouter to handler our requests.

You can use a router of your choice like gorilla/mux but  i personally like httprouter due to its simplicity.

Your will realize some packages  like(kenyaappexperts/database)have been introduced.This are files that i use to manage my database connection.

Now we have successfully created a function to accept request to change a user profile.The last step is to expose this function  and we will do this in the main.go file.

Connecting Our handler function with a router

In this final step we are going to to make a route that any time we want to change a user profiling we will be calling.

In this tutorial you should have noticed we have just retrieved the file without the form data.In my next tutorial i am going to show you how to do this.

That all you need to upload a file in golang. Happy coding .You can enroll for our free courses at Codesahara.

You can also read some of our previous post.Here


2 thoughts on “How to upload files in Go step by step

  1. Hey ,
    Well commented article,very simple explanation.
    However one problem is that , it would be helpful to provide a basic working example ie I don’t have to go and read about database or models.
    It would be pleasant if it were a complete bare bones example of just file uploading.
    Thanks for the article though.

Leave a Comment