ubantu上编辑windows程序

 

命令简记

cd $GOROOT/src

cp -r $GOROOT /root/go1.4

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash

 

操作记录

cd $GOROOT/src

root@ubuntu:/export/app/go/src# CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash
./make.bash: line 165: /root/go1.4/bin/go: No such file or directory
Building Go cmd/dist using /root/go1.4. ()
ERROR: Cannot find /root/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
root@ubuntu:/export/app/go/src# cp -r /export/app/go/ /root/go1.4



root@ubuntu:/export/app/go/src# CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash
Building Go cmd/dist using /root/go1.4. (go1.15 linux/amd64)
Building Go toolchain1 using /root/go1.4.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for host, linux/amd64.
Building packages and commands for target, windows/amd64.
---
Installed Go for windows/amd64 in /export/app/go
Installed commands in /export/app/go/bin

 

测试

package main

import (
    "io/ioutil"
)

func main() {
    content := "看到有很多动物在天上飞,作为一只兔子,我也为此努力了大半生..."

    //如果文件a.txt已经存在那么会忽略权限参数,清空文件内容。文件不存在会创建文件赋予权限
    ioutil.WriteFile("./a.txt",[]byte(content),0777)
}

前面是用root用户设置的,编译时可以用普通用户

hongyun@ubuntu:/export/wks/go/dbm_go/src/test/fil$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ioutil.go 
hongyun@ubuntu:/export/wks/go/dbm_go/src/test/fil$ ll
total 1632
drwxrwxr-x  2 hongyun hongyun    4096 12月  3 14:33 ./
drwxr-xr-x 18 hongyun hongyun    4096 12月  3 14:19 ../
-rwxrwxr-x  1 hongyun hongyun 1656320 12月  3 14:33 ioutil.exe*
-rw-rw-r--  1 hongyun hongyun     341 12月  3 14:29 ioutil.go

 

将程序放到windows上,执行后会在当前目录下生成一个文件

 

 

 

别名

alias win="CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build "

这样编译windows程序就方便多

hongyun@ubuntu:/export/wks/go/dbm_go/src/test/fil$ win ioutil.go 
hongyun@ubuntu:/export/wks/go/dbm_go/src/test/fil$ ll
total 1632
drwxrwxr-x  2 hongyun hongyun    4096 12月  3 14:37 ./
drwxr-xr-x 18 hongyun hongyun    4096 12月  3 14:19 ../
-rwxrwxr-x  1 hongyun hongyun 1656320 12月  3 14:37 ioutil.exe*
-rw-rw-r--  1 hongyun hongyun     341 12月  3 14:29 ioutil.go

 

posted @ 2020-12-03 14:41  方诚  阅读(159)  评论(0编辑  收藏  举报