網頁

2021/12/5

AWS EC2 Linux 2執行Go hello world專案

在AWS EC2執行個體Linux 2執行Go Hello World程式範例。


首先建立一個AWS EC2 Linux 2執行個體,然後在建立的EC2 Linux 2安裝Git安裝Go


參考「我的第一支Go程式 hello world」在本機建立Go hello-world專案並push到GitHub。本範例Go專案的module名稱為abc.com/demo

ec2-user的home目錄/home/ec2-user從GitHub clone hello-world專案,範例為https://github.com/matthung0807/go-demo.git

[ec2-user@ip-172-31-21-26 ~]$ git clone https://github.com/matthung0807/go-demo.git
Cloning into 'go-demo'...
remote: Enumerating objects: 202, done.
remote: Counting objects: 100% (202/202), done.
remote: Compressing objects: 100% (156/156), done.
remote: Total 202 (delta 56), reused 178 (delta 34), pack-reused 0
Receiving objects: 100% (202/202), 37.83 KiB | 267.00 KiB/s, done.
Resolving deltas: 100% (56/56), done.

檢視下載的專案目錄為go-demo

[ec2-user@ip-172-31-21-26 ~]$ ls
go-demo

進入專案go-demo目錄並檢視內容如下。

[ec2-user@ip-172-31-21-26 ~]$ cd go-demo
[ec2-user@ip-172-31-21-26 go-demo]$ ls
README.md  go.mod  main.go

在專案根目錄執行go build abc.com/demo編譯專案為可執行檔demo

[ec2-user@ip-172-31-21-26 go-demo]$ go build abc.com/demo
[ec2-user@ip-172-31-21-26 go-demo]$ ls
README.md  demo  go.mod  main.go

輸入./demo執行demo執行檔,結果印出main.go的內容Hello World

[ec2-user@ip-172-31-21-26 go-demo]$ ./demo
Hello World


沒有留言:

張貼留言