{"id":1349,"date":"2023-07-31T15:48:18","date_gmt":"2023-07-31T07:48:18","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=1349"},"modified":"2023-07-31T15:48:18","modified_gmt":"2023-07-31T07:48:18","slug":"golang%e6%89%93%e5%8c%85dmg%e6%96%87%e4%bb%b6%e5%8e%9f%e7%90%86%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/1349\/","title":{"rendered":"golang\u6253\u5305dmg\u6587\u4ef6\u539f\u7406\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"

\u5728macOS\u7cfb\u7edf\u4e2d\uff0cdmg\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u78c1\u76d8\u6620\u50cf\u6587\u4ef6\u683c\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u8f6f\u4ef6\u7684\u5206\u53d1\u548c\u5b89\u88c5\u3002\u5728golang\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u6253\u5305\u751f\u6210dmg\u6587\u4ef6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u6bd4\u8f83\u6d41\u884c\u7684\u5e93\uff0c\u4ee5\u53ca\u5b83\u7684\u539f\u7406\u548c\u7528\u6cd5\u3002<\/p>\n

1. \u5e38\u7528\u7684dmg\u6253\u5305\u5e93<\/p>\n

\u5728golang\u4e2d\uff0c\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u6765\u6253\u5305dmg\u6587\u4ef6\uff0c\u6bd4\u5982hdiutil\u3001dmgbuild\u3001go-dmg\u7b49\u7b49\u3002\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u4f7f\u7528go-dmg\u5e93\u6765\u8fdb\u884c\u4ecb\u7ecd\u548c\u6f14\u793a\u3002<\/p>\n

go-dmg\u662f\u4e00\u4e2a\u5f00\u6e90\u7684golang\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210dmg\u6587\u4ef6\uff0c\u652f\u6301\u81ea\u5b9a\u4e49dmg\u6587\u4ef6\u7684\u540d\u79f0\u3001\u5927\u5c0f\u3001\u80cc\u666f\u56fe\u3001\u56fe\u6807\u4f4d\u7f6e\u7b49\u7b49\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5f15\u5165\u5e93\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u5373\u53ef\u3002<\/p>\n

2. \u6253\u5305dmg\u6587\u4ef6\u7684\u539f\u7406<\/p>\n

dmg\u6587\u4ef6\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u78c1\u76d8\u6620\u50cf\u6587\u4ef6\uff0c\u7c7b\u4f3c\u4e8eiso\u6587\u4ef6\u3002\u5728macOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528hdiutil\u547d\u4ee4\u6765\u521b\u5efa\u3001\u6302\u8f7d\u548c\u5378\u8f7ddmg\u6587\u4ef6\u3002go-dmg\u5e93\u7684\u539f\u7406\u5c31\u662f\u8c03\u7528hdiutil\u547d\u4ee4\u6765\u521b\u5efadmg\u6587\u4ef6\uff0c\u5e76\u5c06\u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u6dfb\u52a0\u5230dmg\u6587\u4ef6\u4e2d\u3002<\/p>\n

3. \u4f7f\u7528go-dmg\u5e93\u6253\u5305dmg\u6587\u4ef6<\/p>\n

\u4e0b\u9762\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528go-dmg\u5e93\u6765\u6253\u5305dmg\u6587\u4ef6\u3002<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5go-dmg\u5e93\uff1a<\/p>\n

“`<\/p>\n

go get github.com\/andreyvit\/dmg<\/p>\n

“`<\/p>\n

\u63a5\u7740\uff0c\u521b\u5efa\u4e00\u4e2amain.go\u6587\u4ef6\uff0c\u5e76\u5bfc\u5165dmg\u5e93\uff1a<\/p>\n

“`go<\/p>\n

pa\u6253\u5305\u684c\u9762\u7a0b\u5e8f\u8f6f\u4ef6<\/a>ckage main<\/p>\n

import (<\/p>\n

\t“fmt”<\/p>\n

\t“github.com\/andreyvit\/dmg”<\/p>\n

)<\/p>\n

func main() {<\/p>\n<\/p>\n

}<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u5b9a\u4e49dmg\u6587\u4ef6\u7684\u5c5e\u6027\uff0c\u6bd4\u5982\u540d\u79f0\u3001\u5927\u5c0f\u3001\u80cc\u666f\u56fe\u7b49\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528dmg.New\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2admg.File\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u76f8\u5e94\u7684\u5c5e\u6027\u503c\uff1a<\/p>\n

“`go<\/p>\n

file := dmg.New(“MyApp.dmg”, “\/path\/to\/source\/dir”)<\/p>\n

file.SetFormat(dmg.UDRW)<\/p>\n

file.SetSize(“100m”)<\/p>\n

file.SetIconPosition(0, 0)<\/p>\n

file.SetBackgroundColor(dmg.RGBA{255, 255, 255, 255})<\/p>\n

“`<\/p>\n

\u5176\u4e2d\uff0cdmg.UDRW\u8868\u793a\u4f7f\u7528UDRW\u683c\u5f0f\u521b\u5efadmg\u6587\u4ef6\uff0c100m\u8868\u793admg\u6587\u4ef6\u7684\u5927\u5c0f\u4e3a100MB\uff0c(0, 0)\u8868\u793a\u56fe\u6807\u7684\u4f4d\u7f6e\u4e3a\u5de6\u4e0a\u89d2\uff0cRGBA{255, 255, 255, 255}\u8868\u793a\u80cc\u666f\u8272\u4e3a\u767d\u8272\u3002<\/p>\n

\u63a5\u7740\uff0c\u5c06\u9700\u8981\u6dfb\u52a0\u5230dmg\u6587\u4ef6\u4e2d\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u6dfb\u52a0\u5230dmg.File\u5bf9\u8c61\u4e2d\uff1a<\/p>\n

“`go<\/p>\n

file.Add(“\/path\/to\/source\/dir\/MyApp.app”, “Applications”)<\/p>\n

“`<\/p>\n

\u5176\u4e2d\uff0c\/path\/to\/source\/dir\/MyApp.app\u8868\u793a\u9700\u8981\u6dfb\u52a0\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u8def\u5f84\uff0c”Applications”\u8868\u793a\u6dfb\u52a0\u5230dmg\u6587\u4ef6\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n

\u6700\u540e\uff0c\u8c03\u7528dmg.File\u5bf9\u8c61\u7684Write\u51fd\u6570\uff0c\u5c06dmg\u6587\u4ef6\u5199\u5165\u78c1\u76d8\u4e2d\uff1a<\/p>\n

“`go<\/p>\n

err := file.Write(“MyApp.dmg”)<\/p>\n

if err != nil {<\/p>\n

fmt.Println(“Failed to write DMG:”, err)<\/p>\n

}<\/p>\n

“`<\/p>\n

\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

“`go<\/p>\n

package main<\/p>\n

import (<\/p>\n

\t“fmt”<\/p>\n

\t“github.com\/andreyvit\/dmg”<\/p>\n

)<\/p>\n

func main() {<\/p>\n

\tfile := dmg.New(“MyApp.dmg”, “\/path\/to\/source\/dir”)<\/p>\n

\tfile.SetFormat(dmg.UDRW)<\/p>\n

\tfile.SetSize(“100m”)<\/p>\n

\tfile.SetIconPosition(0, 0)<\/p>\n

\tfile.SetBackgroundColor(dmg.RGBA{255, 255, 255, 255})<\/p>\n

\tfile.Add(“\/path\/to\/source\/dir\/MyApp.app”, “Applications”)<\/p>\n

\terr := file.Write(“MyApp.dmg”)<\/p>\n

\t\u628aweb\u9879\u76ee\u6253\u5305exe<\/a>if err != nil {<\/p>\n

\t\tfmt.Println(“Failed to write DMG:”, err)<\/p>\n

\t}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u540d\u4e3aMyApp.d<\/p>\n

<\/figure>\n<\/p>\n

mg\u7684dmg\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u4e86\/path\/to\/source\/dir\/MyApp.app\u6587\u4ef6\u3002<\/p>\n

4. \u603b\u7ed3<\/p>\n

\u901a\u8fc7\u4f7f\u7528go-dmg\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210dmg\u6587\u4ef6\uff0c\u5e76\u81ea\u5b9a\u4e49dmg\u6587\u4ef6\u7684\u5c5e\u6027\u548c\u5305\u542b\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u5b83\u7684\u539f\u7406\u662f\u8c03\u7528hdiutil\u547d\u4ee4\u6765\u521b\u5efa\u548c\u64cd\u4f5cdmg\u6587\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u5e93\u6765\u8fdb\u884c\u6253\u5305\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728macOS\u7cfb\u7edf\u4e2d\uff0cdmg\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u78c1\u76d8\u6620\u50cf\u6587\u4ef6\u683c\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u8f6f\u4ef6\u7684\u5206\u53d1\u548c\u5b89\u88c5\u3002\u5728golang\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u6253\u5305\u751f\u6210dmg\u6587\u4ef6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u6bd4\u8f83\u6d41\u884c\u7684\u5e93\uff0c\u4ee5\u53ca\u5b83\u7684\u539f\u7406\u548c\u7528<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3038,766,16,25,611],"topic":[],"class_list":["post-1349","post","type-post","status-publish","format-standard","hentry","category-exekf","tag-beet","tag-766","tag-16","tag-25","tag-611"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=1349"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1349\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=1349"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}