So I’ve been working on a project that uses the Go programming language. The application I’m building is a web application that I plan to distribute. The thing is, I don’t want to distribute hundreds of files to make it possible. The application has an API built with Go and the front-end that consumes the API is built with Angular. This lead me searching for a way to bundle all the files into the final binary.
Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard. Note: I only need the file URLs, I do not need to access the actual files. Mar 17, 2017 Bundle the Assets that will be Served. The way the go.rice package works is it looks at our source code for the MustFindBox function. It finds the website directory and understands that we need to bundle that directory. To bundle the assets we need to use a separate application, not the standard go build that we’ll do soon.
There are several packages that exist for bundling assets into a Golang application. Popular packages include go-bindata, go-bindata-assetfs, and go.rice. I had the most success with go.rice, so we’re going to explore it in this article.
Now I’m building a Go with Angular application, but I think that will yield too complex of an example for this article. Instead we’re going to build a very simple application that uses Golang, HTML, CSS, and JavaScript.
Create a Project and Download the Dependencies
Before going any further, the assumption is that you have Go installed and the proper paths configured. With that said, create a myproject somewhere on your computer.
This project should contain the following files and directories:
If you don’t have the
mkdir
and touch
commands, just create the files manually. There are a few packages that must be installed now. Execute the following:The mux library will make it easier for us to work with routes, but if you really wanted to, you could just use the default Golang libraries. If you want to learn more about creating an API with the mux library, you can check out my previous tutorial on the subject.
At this point in time we can develop the application.
Develop the Application for Serving Assets
Golang Mac App Bundle Android
We’re going to start by developing the website portion of the application before the Golang portion. Just to demonstrate bundling all kinds of files, we’re going to work with HTML, JavaScript, and CSS.
Let’s start with the styles. Open the project’s website/custom.css file and include the following:
Essentially we just want a colored container with vertical aligned text in it. Now let’s take a look at the small bit of application JavaScript.
Open the project’s website/app.js file and include the following JavaScript code:
The above line will look for a UI component with an id of
text
and fill it with HTML.Now let’s bring it together with our very simple HTML markup. Open the project’s website/index.html file and include the following markup:
X-Mirage is a professional AirPlay server for Mac and Windows PC, which allows you to wirelessly stream contents or mirror iPhone, iPad and iPod screen to any Mac/Windows PC. X-Mirage Deals scours the web for the newest software, gadgets & web services. Explore our giveaways, bundles, Pay What You Want deals & more. X-Mirage is the most professional AirPlay server for Mac and Windows PC, which allows you to wirelessly stream contents or mirror iPhone, iPad and iPod screen to any Mac/Windows PC. Record screen and audio from iOS devices, as well as voiceover via Microphone with one click. Support 4K Quad Full HD Mirroring. Compatible with iOS 14. X-mirage mac app. 2013/9/30 X-Mirage for Mac 1.01.5 released! Mirror iOS devices screen to Mac. Stream movies, music and photos from iOS devices to Mac. Record iOS screen on Mac. X-Mirage is a professional Airplay server for Windows/Mac that allows you to receive Airplay from iPhone, iPad, iPod Touch. Use it to mirror iPhone/iPad/iPod Touch to PC/Mac and record screen activities with sound&voiceover.
Again, nothing very complicated as of now. We’re just demonstrating a website with several file components to it. A perfect candidate for bundling into a Golang application.
Now let’s check out our Go code. We won’t have any API endpoints in this application, but we could if we wanted to. Open the project’s main.go file and include the following code:
The most important line in this file is the one where we use the go.rice component. What we are doing is we are saying that we want our assets found in website to be served and accessible from the root path of our served application. This means that when we serve our application, we can access it from http://localhost:12345/.
We’re not quite done. We know where our files are, but the application will not be truly reading from those files. Instead we need to bundle these assets into a Go file that can be compiled into our application.
Mac app always open second desktop. Apps can now be fully controlled using just the keyboard. Even more powerful.The new APIs and behaviors in macOS Big Sur let you create even more powerful Mac apps. You can create out-of-window and detachable popovers, control window tabbing using new window APIs, and make it easier for users to select photos in your app by using the updated Photos picker. IOS Photos editing extensions can now be built to run on Mac.
Bundle the Assets that will be Served
Golang Android App
The way the go.rice package works is it looks at our source code for the
MustFindBox
function. It finds the website directory and understands that we need to bundle that directory.To bundle the assets we need to use a separate application, not the standard
go build
that we’ll do soon. We’ve already downloaded the separate application. If it isn’t already in your path, add the following:Depending on your operating system, the above command could be different. This will allow us to do the following:
The above command will create a file called rice-box.go in your project. Open it and see what it contains. You’ll notice that it contains the data to each of your website files.
When you build your Go application, the website data will be built in. That means you only need to pass around one executable application.
Conclusion
![Top mac app Top mac app](/uploads/1/3/4/2/134262043/226998214.png)
You just saw how to bundle web assets into a Golang application. There are plenty of reasons why this would be useful. One example is that you’d like to distribute one application file that has a web interface built in. This is perfect for building cross platform applications that have a UI rather than just a CLI.
Close all open programs A little-known set of keystrokes will shut down all active programs at once in no time. Press Ctrl-Alt-Delete and then Alt-T to open Task Manager's Applications tab. This key combination will let you scroll through app icons to switch between apps currently open and running on your Mac. Make sure the application switcher is on the app you want to close. Scroll through apps in the switcher until you're on the correct app. Press ⌥ Option+ ⌘ Command+W on your keyboard. Press the three buttons at the same. How to close an open app in mac. How to Quit apps/programs in Mac - latest macOS. How to Quit apps/programs in Mac - latest macOS. The best option, in our opinion, comes in the form of the wonderful Option key. To close all of an app’s windows in one fell swoop, just press Command+Option+W, or open the “File” menu and hold the “Option” key to see the Close All command. More of a mouse user? There’s another secret power the Option key will unveil. Click on the Apple icon in the top menu bar of your Mac and then click on Force Quit Application in the drop-down menu (See image below). This will open the Force Quit Applications manager which will show you all running apps on your Mac 3.
Want more information on building an API for this bundled application? Check out this full stack tutorial I wrote on the subject.
Nic Raboy
Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.
Please enable JavaScript to view the comments powered by Disqus.