Skip to content

Go

Goのredirect

ログアウトの処理(GET /logout)実行後にトップページにリダイレクトしようとした。

http.Redirect(w, r, “/”, 301)

おや?動作しない時がある。。

301だと、ブラウザはリダイレクト先を覚えて、次からはリダイレクト先を最初から叩くっぽい。

なので、302にしてあげないと思ったような動きにならない。

ところで、Goのhttpパッケージに定義されているステータスコードhttp.StatusTemporaryRedirect307なんだけど、これはPOSTの処理のリダイレクトもPOSTで行う(元のメソッドと同じにする)ので、トップページとかGETしか定義していないところへのリダイレクトがエラーになる。

Read more

GoのJSON

PocketBaseのユーザー作成をGoからHTTP叩いて実行しようとして、正常時とエラー時で戻ってくるJSONの形式が違っていた。

structを別々に用意して、Unmarshalでエラーになったらもう片方を使う?とか思ったけど、 golang は ゆるふわに JSON を扱えまぁす! — KaoriYaによると、interface{}(Go1.18以降ならanyか)を使えば、structを事前に用意する必要がないと。

Read more

Airを試す

HTMLとかコード修正したときに手動で再起動するのが面倒なので、ホットリロードツールを導入してみた。

$ 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

Revel入門

ちょっとしたWebアプリを作りたくなったので、GoでRailsみたいなフレームワークないのかなと探した。

が近そうだったので、試してみた。

$ 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で接続するイメージの軽量化

go-oci8を使ったイメージの、oraclelinux8をベースイメージにしたやつが1.25GBにもなったので、もっと小さくならないか試してみた。

golang:1-bullseyeをベースにした場合、rpmが使えないのでOracleInstantClient関連のzipを展開する方法でやって、1.32GB。増えてる。

Read more

OracleにGoで接続する3

昨日は、mattn/go-oci8: Oracle driver for Go using database/sqlsijms/go-ora: Pure go oracle clientを使ったサンプルをコンテナ上でgo runして動かしていた。

今日はgo buildでバイナリにして実行できるようにしてみる。

何もオプション付けずにビルドしたら、どちらも問題なし。

バイナリサイズを小さくしようと、-ldflags="-s -w -extldflags \"-static\""をつけたところ、go-oraは大丈夫。go-oci8はNGだった。

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
    command: bash

sijms/go-oraのサンプルファイルを拝借。 importの所はv2に修正。

sh-4.4# vi main.go
sh-4.4# go mod tidy
go: finding module for package github.com/sijms/go-ora/v2
go: downloading github.com/sijms/go-ora/v2 v2.4.24
go: found github.com/sijms/go-ora/v2 in github.com/sijms/go-ora/v2 v2.4.24
sh-4.4# go run main.go  oracle://system:OraclePwd@db:1521/XE

Successfully connected.

接続はできているみたいだけど、SQLの実行結果が表示されない。。

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": executable file not found in $PATH
sh-4.4# yum install -y gcc
sh-4.4# go run go-oci8/main.go 
# github.com/mattn/go-oci8
In file included from vendor/github.com/mattn/go-oci8/c_helpers.go:3:
./oci8.go.h:1:10: fatal error: oci.h: No such file or directory
 #include <oci.h>
          ^~~~~~~
compilation terminated.
sh-4.4# find / | grep oci.h
/usr/include/oracle/21/client64/oci.h

oci8.pcのincludedirを/usr/include/oracle/21/client64に変更。

Read more