The GO program's packaging capabilities on MacOS

Source: Internet
Author: User
This article simply explains how to package a Go program on MacOS, including referencing or not referencing external resources. As a native Cocoa program it can be downloaded, installed and run. The development process does not require XCODE,CGO or other special library files. At the end of this article, your Go program will be packaged into an '. App ' file to become a drag-and-drop, installation of a read-only DMG file. This also applies to other non-Go language programs. There are many other guidance articles on this end-to-end solution, some more than I want to do, some in different ways, and what I want to present to you is the process of packaging the files. * * Required * *: You have to have a macOS. This method does not take effect on Windows or Linux. In fact, it may also work, but you have to do it manually or with some crazy tools to make things that are related to MacOS, such as DMG or '. Ds_store ' file. # # Make an icon for your app you need a high-resolution icon; it's better to be nice. Need to perform well on both light and dark backgrounds. [See Apple's official documentation] (https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/ optimizing/optimizing.html). For best results, save the icon as a transparent PNG file with 1024x1024 resolution. Here are some examples:! [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/1.png) # # # Compile icon set when large icons are ready, You need to save it in different sizes and resolutions. For better compatibility, the recommended size lists are: 16,32,64,128,256,512 and 1024, and each size requires a ' @2x ' icon for high-resolution scenes (except 1024). If you do not have a graphical program to help you do this, it will be very tedious and lengthy, or you can use the command shown below (SIPs) to do the resolution adjustment work: "' shell$ sips-z $SIZE $SIZE myicon.png \--out myicon_$ {Size}x${size}.png "and so on. Don't forget ' @2.X ' batches, which have a higher level of size but are labeled as the current name. This is really tedious work, but it can be automated, and you only have to do it once (a sample program is linked at the end of this article). Next, suppose your icon file is located in a folder called Myicons, using [Iconutil] (https://developer.apple.com/library/content/documentation/ Graphicsanimation/conceptual/highresolutionosx/optimizing/optimizing.html#//apple_ref/doc/uid/ TP40012302-CH7-SW2) to generate the icon: "' shell$ iconutil-c icns-o \ Icon.icns myicons.iconset ' ' # # # made. App Bundlemacos Can load a running program is just one. The app's folder contains a binary file and a manifest file. You can even create them manually. Its directory structure is similar to the following: "' shell$ tree Caddy.appcaddy.app/└──contents├──info.plist├──macos│└──caddy└──resources└──icon.icns3 Directories, 3 Files "key file contains: + info.plist: Manifest file. Can be copied from other places to customize + Caddy: You want to package the binary file + Icon.icns: Package The different size of the icon file (right or Ctrl-click the program icon, select "Show Package Contents" can browse. App folder) # # # Add The Info.plist file shows a manifest file that you can modify according to your own content ([from Dmitri Shurylov] (https://github.com/shurcooL/trayhost), thanks to the original author) ' XML <?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" ><plist VersioN= "1.0" ><dict> <key>CFBundleExecutable</key> <string>caddy</string> <key> Cfbundleiconfile</key> <string>icon.icns</string> <key>CFBundleIdentifier</key> <string>com.example.yours</string> <key>NSHighResolutionCapable</key> <true/> < Key>lsuielement</key> <true/></dict></plist> "Please make sure you have replaced [cfbundleexecutable] (https:// developer.apple.com/library/content/documentation/general/reference/infoplistkeyreference/articles/ corefoundationkeys.html) and [Cfbundleidentifier] (https://developer.apple.com/library/content/documentation/ general/reference/infoplistkeyreference/articles/corefoundationkeys.html). Note: [Lsuielement] (https://developer.apple.com/library/content/documentation/General/Reference/ infoplistkeyreference/articles/launchserviceskeys.html) tells the operating system that your program is an agent, so he will not appear in the taskbar. This is important, otherwise it will always be repeated in the taskbar, unless your Go program Imaging Cocoa program responds to events in the Mac event loop. Congratulations to you! Your Go app already has a good package '. App '. Double-click the icon after itwill be run. You can drag it to the application folder and it will appear in your launcher like a Cocoa program. # # # # # # # # of DMG files you're distributing programs to. It compresses the entire '. App ' file and can be easily dragged to the application folder and installed. This tutorial shows you the manual authoring process, and the main point behind this is to show you how to automate the task. # # # Making a DMB template template needs to be made only once. Open the Disk utility. Press ⌘N to create a new disk image. Give it a name and configure enough space to accommodate your package. [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/2.png) Locate the mounted image in the Finder. Customize the view settings for this folder to make it look like you want the user to actually install. Consider setting a background map, hide toolbars and sidebar, add icon size, and more. Note: The background map must be included in the DMG. It is usually placed in the '. Background ' folder. The folder name begins with a dot, and it appears as a hidden folder. Place the background image here and drag and drop it into the view options to configure it. [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/3.png) For the sake of convenience, you may need for DMG/ Create a shortcut (alias) for the application folder. You can right-click on the/application folder, select "Create Alias" and move to the attached image. [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/4.png) Now we can add the packaged app to the DMG To complete the customization of the view:! [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/5.png) Good job! Our template DMG has all been built. Now it's ready for distribution. # # Convert DMG files to facilitate distribution the current DMG is not compressed and is writable. This is not enough for the release process, so we have to do a bit of turningIn exchange for repair. Open the Disk utility and select mirror-to-convert. Give the file a meaningful name, and the other settings remain the same. (The image format should be "compressed")! [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/6.png) Look! You now have a zip package that you can publish. When you open this file, drag it to the application to create a shortcut, it will appear in the Launcher:! [Avatar] (https://raw.githubusercontent.com/studygolang/gctt-images/master/package-for-mac/7.png) "Oh, it's beautiful!" ”... Except a little monotonous. :) # # Future image making, you can reuse this template DMG and icons. All the painful work needs to be done only once. In order to publish again, you just need to repackage. The app (can be automated), replaced with the template DMG (can be automated) and re-convert DMG to distribute (also can be automated). # # Automating process automation Some of the one-time tasks described above can be automated (such as creating an icon library). But it's hard to get good results with automation (such as customizing the DMG view). Fortunately, the rest of it is easy to automate, including the steps needed to repackage a new release version each time. Hdiutil This command can help you to create, hang, and convert images. Here (https://gist.github.com/mholt/11008646c95d787c30806d3f24b2c844) is an example of an automation I wrote. It will create a packaged. App, if you give it a template. DMG It can create the final. DMG. It even encapsulates the commands for creating icons described above, which can help you create all the different sizes of icons. It does all the copying, mounting, unloading, and mirroring conversions. You can make changes to parts you don't like or where it doesn't work (very likely, haha). It has very good notes and is easy to read. You just need to tell it where the binaries are, where the resource files are, the names of the binary files, the 1024-pixel icon file, and the meaningful program name. This is not an open source project that will continue to be maintained. If you want to use it, you can integrate it into the actual application scenario. I think it's really cool to let more Go projects run in the form of apps. The Go code is beautiful, so why not make the compiled binaries equally beautiful?

via:https://medium.com/@mattholt/packaging-a-go-application-for-macos-f7084b00f6b5

Author: Matt Holt Translator: lebai03 proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

151 Reads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.