LoginSignup
18
12

More than 5 years have passed since last update.

Go言語でAmazon Linux AMI向けのビルド

Last updated at Posted at 2015-06-23

自分用メモです〜

通常ビルドとデプロイ

Macでビルドしたものをscpコマンドで転送してサーバー上で実行しても

$ go build hello.go

これでhello.goからhelloという実行形式ファイルが生成されます。

$ ls
hello.go hello

とりあえずMacで実行は出来ます。

$ ./hello
hello go!!

サーバー(EC2)に転送します。
EC2側はAmazonLinuxAMIです。

$ scp -i ~/.ssh/hogehoge.pem ./hello ec2-user@xx.xx.xx.xx:~/
$ ./hello
-bash: ./hello: バイナリファイルを実行できません

となり実行できません

Linux向けビルド

参考までにEC2管理画面の情報です。

Jxck_さんの記事(Go のクロスコンパイル環境構築)をもとに環境を整えます。

  • Mac側で一度Goを入れ直しました。(入れ直さなくてもいけた?)
$ brew uninstall go
  • オプションをつけてGoを再インストール (詳しくは参考記事参照)
$ brew install go --cross-compile-common
  • hello.goをビルドします。GOOSGOARCHを指定してビルドします。
$ GOOS=linux GOARCH=amd64 go build hello.go

これでLinux向けのhelloというバイナリが出来ます。

ちなみにMac側でビルドしましたが、Linux向けにビルドしているのでMacでは実行できません。

$ ./hello
zsh: exec format error: ./hello

scp転送して、サーバーで実行してみましょう。

$ ./hello
hello go!!

実行できました!

18
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
12