-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kadai4 misonog #57
base: master
Are you sure you want to change the base?
Kadai4 misonog #57
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いくつかコメントしてみました
|
||
func (o *omikuji) Draw() { | ||
if isShogatsu(o.date) { | ||
o.result = daikichi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここでreturnしてしまえば、elseはいらないですね
if isShogatsu(o.date) { | ||
o.result = daikichi | ||
} else { | ||
rand.Seed(time.Now().UnixNano()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand.Seedみたいな共通の初期化系の処理は、init
functionで最初に実行させてしまうという方法があります
if dateValue != "" { | ||
date, err := time.Parse(timeForm, dateValue) | ||
if err != nil { | ||
log.Fatal(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この場合、httpのerrorとして返した方が良いと思います。特に、ユーザーが異常な値を入れたら発生する想定内なエラーだと思うので
handler(w, r) | ||
rw := w.Result() | ||
defer rw.Body.Close() | ||
if rw.StatusCode != http.StatusOK { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StatusCodeも期待値に入れると、異常系のテストもできます Bad Requestのときとかも含め
type omikuji struct { | ||
result string | ||
date time.Time | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この2つのデータをstructとしてまとめて置く必要はありますか?
daikichi = "大吉" | ||
) | ||
|
||
var omikujiResults = []string{"吉", "小吉", "凶"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
daikichi以外の値も定数に定義して、daikichiも含めてこのsliceに入れてしまう方が可変性、可読性(一覧性)ともに高いと思います。
} | ||
|
||
_, month, date := t.Date() | ||
if month == time.January { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここがfalseの時点で return false
すれば、ifをネストしなくてよくなりますね
) | ||
|
||
func main() { | ||
http.HandleFunc("/", handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これをmain関数の外に置いておくことでhttpのroutingも含めてテストできるようになります
@sryoya レビューありがとうございます!別途確認いたします。 |
おみくじ API
お手数ですがレビューお願いいたします。
仕様
利用方法
setup
$ make # テスト & ビルド
サーバーの起動
おみくじを引く
ディレクトリ構造
. ├── Makefile ├── README.md ├── go.mod ├── main.go ├── omikuji-server ├── omikuji.go └── omikuji_test.go