[golang] How to use github for hosting self-updating binaries for your golang projects
- Create a new repo to host your binaries. (Artifactory)

2. Clone the repository on your local machine.


3. It’s a good idea to start with branch named gh-pages
to serve the content.
git checkout -b gh-pages
4. Create a folder for your project, for demonstration purpose, let’s use hello-updater
as it comes as an example with the go-selfupdate project.
5. Add an index.html
file.
6. Once created, commit the file and push the branch to the repository.
7. Validate that everything is working fine by going to the URL: https://<username>.github.io/artifactory/hello-updater/index.html
8. Clone the project: go-selfupdate by running below command:
git clone git@github.com:sanbornm/go-selfupdate.git
9. Install go-selfupdate binary to your system by running below command:
go get -u github.com/sanbornm/go-selfupdate/...
10. Change to example/src/hello-updater directory and update the main.go
file with your URL:
ApiURL: “https://<username>.github.io/artifactory/"
and run the below command to generate the binary with version 1.1
go build -ldflags="-X main.version=1.1" -o hello-updater src/hello-updater/main.go
11. And, it will create the file named hello-updater
in the same folder as an executable, and on running it will give the below output:
$ ./hello-updater
2022/07/03 17:53:21 Hello world I am currently version 1.1
2022/07/03 17:53:23 Next run, I should be
12. Make a deployment
directory inside example
directory, if not present and move the binary inside it and also run the below command to have it in our artifacts
go-selfupdate -o public/hello-updater/ hello-updater 1.1
13. Next step is to generate an updated binary (you can change the code) or just change the version number by running below command:
go build -ldflags="-X main.version=1.2" -o hello-updater src/hello-updater/main.go
14. And, now we need to update it using go-selfupdate
by running below command:
go-selfupdate -o public/hello-updater/ hello-updater 1.2
15. The above command will create a folder named hello-updater
with version number
in the public
directory. There is also a json file created with the OS details and gz file of your binary in the version
folder.
16. Copy all the contents to the artifactory
repo, inside project directory

17. Now, commit and push it. Validate that windows-amd64.json
is accessible
https://<username>.github.io/artifactory/hello-updater/windows-amd64.json
18. Now, try to run the local binary present in deployment, and observe the output

19. And, run it again to get the updated version

20. That’s it. Easily, self updating binaries.
Please hit a clap
icon, if you enjoy the post. Thank you!