Closed14

Golang 事始め

otokunaga2otokunaga2

ORMメモ

https://gorm.io/docs/
GORMの解説記事
https://qiita.com/gold-kou/items/45a95d61d253184b0f33

ent

GORMよりも後から開発されている?ORM
Facebookのエンジニアの方が開発されている。
複雑なデータモデルを扱うケースなどに良さそう。

ent はGoのためのシンプルかつ強力なエンティティフレームワークです。このフレームワークを使うことで,大量のデータモデルをもつアプリケーションのビルドとメンテナンスを簡単に行うことができ、次のような原則に従うことができます:

https://entgo.io/ja/blog/2019/10/03/introducing-ent/

公式ドキュメント

日本語化も進んでいるようです。
https://entgo.io/ja/docs/code-gen/

entの使い方

entの使い方は次のmattnさんの記事が参考になります。
https://zenn.dev/mattn/articles/c08072b42f7a5cdcd749

otokunaga2otokunaga2

画像処理について

以下のチュートリアルを参考に実装

https://docs.google.com/presentation/d/1Vt8RQedXPd2RRyVVmO0eBmtrqJzkup3dMwnMEkX-Pq8/edit#slide=id.g28ad670fe0_0_973

メモ

以下のサイトを参考に初期化処理を以下のように加えた。
初期化処理を実施しないと、画像を読み込む際に、unknown format errorが発生した。
このエラーは、画像処理をする際に最初に読み込む画像ファイルのフォーマットを指定しないと、おそらくライブラリ内でフォーマットが指定されず処理できない?ことによるエラーかと推測している。

2021/03/13 20:14:53 image: unknown format

https://socketloop.com/tutorials/golang-how-to-read-jpg-jpeg-gif-and-png-files

ちなみにinit関数はmain関数の前に処理を実施できる関数であり、利用するライブラリの初期化処理などに利用できるとのこと。
以下のサイトがinit関数について非常にわかりやすくまとめてあった。
https://qiita.com/tenntenn/items/7c70e3451ac783999b4f

otokunaga2otokunaga2

アサインメントについて

https://tour.golang.org/basics/10

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

関数内で:=と表記された際には、暗黙的な型として宣言できる。

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

関数外では、すべての式はキーワード(var, funcなど)で始まり、:=は利用できない。

otokunaga2otokunaga2

Go言語に関するFAQ

なぜGo言語は型を変数名の後に記載するのか?

StackOverflowの以下の回答が参考になる。

That matters because putting the optional parts of an expression farther to the right reduces parsing ambiguity, and increases consistency between the expressions that do use that part and the ones that don't.

https://softwareengineering.stackexchange.com/questions/316217/why-does-the-type-go-after-the-variable-name-in-modern-programming-languages
また、Go言語でなぜそのように文法を設計しているのかクリアに記載されていて読むと
設計指針の理解につながりそう。
https://golang.org/doc/faq#declarations_backwards

otokunaga2otokunaga2

Secure Coding Guide

以下のような形でGoのtipsがまとめられている。
Codingで困ったときに参考になりそう。

  • validation
  • connection pool
  • 同時代入
  • 他言語から入ってきた人がよく扱う間違い(ループ内でdeferを利用)

https://golang.shop/

otokunaga2otokunaga2

go getしたコマンドがコンソール上で使えない

原因:go getで取得したコマンドは$GOPATH/binに配置されている。
PATHが上記で設定されていなかったため、タイトルの事象が起きていた。

解決方法:PATHに$GOPATH/binまで含めるようにすることで解決した。

otokunaga2otokunaga2

Goのディレクトリの設定について

以下のリポジトリが説明もあり参考になる。
https://github.com/golang-standards/project-layout
非公式のものではある
srcディレクトリは作成してはいけない。
シンプルなプロジェクトではやりすぎである。

If you are trying to learn Go or if you are building a PoC or a simple project for yourself this project layout is an overkill. Start with something really simple instead (a single main.gofile andgo.mod is more than enough)

参考サイト

https://techdo.mediado.jp/entry/2019/01/18/112503
https://qiita.com/vengavengavnega/items/2235589445dd0effda05

このスクラップは2ヶ月前にクローズされました