Skip to content

go

Goのredirect

ログアウトの処理(GET /logout)実行後にトップページにリダイレクトしようとした。 http.Redirect(w, r, “/”, 301) おや?動作しない時がある。。 301だと、ブラウザはリダイレクト先を覚えて、次からはリダイレクト先を最初から叩くっぽい。 なので、302にしてあげないと思ったような動きにならない。 ところで、Goのhttpパッ Read more

Goのtemplateのinclude

テンプレートのincludeってどうやるんだっけ、と思って検索した。 Go の html/template でヘッダーやフッター等の共通化を実現する方法 · Yutaka 🍊 Kato なるほど、{{define "header"}}~{{end}}で定義して、{{template "header" .}}で呼び出すのか。

GoのJSON

PocketBaseのユーザー作成をGoからHTTP叩いて実行しようとして、正常時とエラー時で戻ってくるJSONの形式が違っていた。 structを別々に用意して、Unmarshalでエラーになったらもう片方を使う?とか思ったけど、 golang は ゆるふわに JSON を扱えまぁす! — KaoriYaによると、interfa Read more

Airを試す

HTMLとかコード修正したときに手動で再起動するのが面倒なので、ホットリロードツールを導入してみた。 cosmtrek/air: ☁️ Live reload for Go apps $ go install github.com/cosmtrek/air@latest $ air init $ air __ _ ___ / /\ | | | |_) /_/--\ |_| |_| \_ , built with Go mkdir /home/umemak/workspace/eventsite_go/tmp watching . watching cmd watching cmd/eventsite watching db watching db/sql watching model watching model/user !exclude tmp watching web watching web/template building... no Go files in /home/umemak/workspace/eventsite_go failed to build, error: exit status 1 ^Ccleaning... see you again~ デフォルトだと、cmdの下のmain.goを見つけてく Read more

Goアプリ

結局、フレームワークはgo-chi/chi: lightweight, idiomatic and composable router for building Go HTTP servicesを使うことにした。 理由は、認証で使いそうなパッケージgo-pkgz/auth: Authenticator via oauth2, direct, email and telegramのサンプルが使っていたから。 とりあえず、HTTPリクエストを受けてDB読み書きしてテンプレート加工して返すところまで Read more

Revel入門

ちょっとしたWebアプリを作りたくなったので、GoでRailsみたいなフレームワークないのかなと探した。 revel/revel: A high productivity, full-stack web framework for the Go language. が近そうだったので、試してみた。 $ go install github.com/revel/cmd/revel@latest go: downloading github.com/revel/cmd v1.1.2 go: downloading github.com/agtorre/gocolorize v1.0.0 go: downloading github.com/jessevdk/go-flags v1.4.0 go: downloading github.com/revel/config v1.1.0 go: downloading github.com/revel/log15 v2.11.20+incompatible go: downloading github.com/mattn/go-colorable v0.1.8 go: downloading gopkg.in/natefinch/lumberjack.v2 v2.0.0 go: downloading gopkg.in/stack.v0 v0.0.0-20141108040640-9b43fcefddd0 go: downloading github.com/pkg/errors v0.9.1 go: downloading github.com/fsnotify/fsnotify v1.4.9 go: downloading github.com/mattn/go-isatty v0.0.14 go: downloading github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac $ revel new -a events_go -r revel: command not found おや?パスが通って Read more

OracleにGoで接続する3

昨日は、mattn/go-oci8: Oracle driver for Go using database/sqlとsijms/go-ora: Pure go oracle clientを使ったサンプルをコンテナ上でgo runして動かしていた。 今日はgo buildでバイナリにして実行できるようにしてみる。 何もオプション付けずにビルドしたら、どちらも問題なし。 バイナリサ Read more

OracleにGoで接続する

前の記事でsqlplusで接続できたので、Goのプログラムから接続してみる。 instantclientベースでGoをインストールするDockerfile作成 FROM ghcr.io/oracle/oraclelinux8-instantclient:21 RUN yum install -y wget tar RUN wget https://go.dev/dl/go1.18.3.linux-amd64.tar.gz RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz ENV PATH="${PATH}:/usr/local/go/bin" CMD [ "go", "version" ] docker-composeもそれを使うように修正 version: '3' services: db: image: container-registry.oracle.com/database/express ports: - 1521:1521 environment: - ORACLE_PWD=OraclePwd cli: build: . image: oraclegocli tty: true Read more

OracleにGoで接続する2

mattn/go-oci8: Oracle driver for Go using database/sqlでやってみる。 sh-4.4# go run go-oci8/main.go # pkg-config --cflags -- oci8 pkg-config: exec: "pkg-config": executable file not found in $PATH sh-4.4# yum install -y pkgconfig sh-4.4# go run go-oci8/main.go # pkg-config --cflags -- oci8 Package oci8 was not found in the pkg-config search path. Perhaps you should add the directory containing `oci8.pc' to the PKG_CONFIG_PATH environment variable Package 'oci8', required by 'virtual:world', not found pkg-config: exit status 1 とりあえずmain.goと同じところにoci8.pcを作成。中身はExamples。 sh-4.4# export PKG_CONFIG_PATH=. sh-4.4# go run go-oci8/main.go # github.com/mattn/go-oci8 cgo: C compiler "gcc" not found: exec: "gcc": Read more