ラベル go の投稿を表示しています。 すべての投稿を表示
ラベル go の投稿を表示しています。 すべての投稿を表示

2019年5月7日火曜日

goサブパッケージの作り方

以下の構成のパッケージ作成を例とします。
├── models
│   ├── common
│   │   └── callAPI.go
・まず「common」というサブディレクトリを作成します。
・次に「callAPI.go」ファイルを作成します。callAPI.goのファイル先頭に「package common」を追加します。
・コマンドラインで「common」の配下に移動した後、「go install」を実行します。

※自分は最初「go install」で実行したら、以下のエラーが表示されました。
 expected 'package', found 'EOF'
理由はファイル「callAPI.go」を保存していなかったからでした。
エディター「Visual Studio Code」を使っているなら、「ファイル」→「保存」をしないと自動的に保存してくれないようでした。

2019年4月19日金曜日

Macでgo言語のウェブアプリケーション開発環境のインストール及び設定

1. まず、インストール(Homebrewがインストール済みを前提にします)


$ brew install go
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
.
.(略)
.
==> Deleted Formulae
safe
==> Downloading https://homebrew.bintray.com/bottles/go-1.12.4.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/99/9920e9264e80f0bac5098a0bbdbd1818c2c44eba37d3b9accd61c1236fcad5f2?__gda__=exp=1555645429~hmac=e139494e5eae781
######################################################################## 100.0%
==> Pouring go-1.12.4.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/go/1.12.4: 9,798 files, 452.6MB

2. 癖のようでバージョンを確認する


$ go version
go version go1.12.4 darwin/amd64

3. $GOPATHの設定


echo 'export GOPATH=$HOME/go' >> ~/.bash_profile
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bash_profile
source ~/.bash_profile

4. beegoをインストール


$go get -u github.com/astaxie/beego
$go get -u github.com/beego/bee
※ 普通に行けると思ったが、以下のエラーができました。gitがないと怒られました。
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/astaxie/beego: exec: "git": executable file not found in $PATH
gitをインストールします。→gitのインストール方法を略します。
無事にgitをインストールできたら、再度上記のbeegoインストールコマンドを実行します。今度無事に実行できたようです。
上記実効後、以下のようなフォルダが作成されたら、ダウンロードできた証拠です。
ls  ~/go/src/github.com/
astaxie/  beego/  shiena/

5. プロジェクトを作成


※今回のプロジェクト名は[test-web]とした場合
$ bee new test-web
上記実行すると、[~/go/src/]の配下に[test-web]というプロジェクトが作られたはずです。

6. プロジェクトを実行


作られたプロジェクトの配下に移動した後、[bee run]を実行します。
cd ~/go/src/test-web/
~/go/src/test-web
$ bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0
2019/04/19 14:08:12 INFO     ▶ 0001 Using 'test-web' as 'appname'
2019/04/19 14:08:12 INFO     ▶ 0002 Initializing watcher...
test-web/controllers
test-web/routers
test-web
2019/04/19 14:08:16 SUCCESS  ▶ 0003 Built Successfully!
2019/04/19 14:08:16 INFO     ▶ 0004 Restarting 'test-web'...
2019/04/19 14:08:16 SUCCESS  ▶ 0005 './test-web' is running...
2019/04/19 14:08:19.753 [I] [asm_amd64.s:1337]  http server Running on http://:8080

7. ブラウザーで確認


ブラウザーのアドレスに[http://localhost:8080/] を入力、以下のように表示されるはずです。