My approach to learning a new language as an experienced developer (Go)
Summary
TLDRこのビデオでは、Go言語を学ぶ方法について紹介します。ソフトウェア開発の経験がある開発者向けに、Go言語を始めとする新しい言語を学ぶ際のアプローチを解説します。まず、Go言語のプロジェクト設定が簡単であることから、すぐにコードを書けるようにし、テストを活用してコードを迅速に試行錯誤できるようにします。次に、Webサーバーの作成、JSONの扱い、SQLデータベースとの連携について学び、実際にAPIを作成して動作を確認します。さらに、依存性の扱いやHTTPリクエストの送信についても触れ、Dockerコンテナを使ったローカル開発のスムーズな方法を提案しています。
Takeaways
- 😀 スクリプトはGo言語を学ぶ方法についての紹介であり、特にソフトウェア開発の経験を持つ開発者を対象としている。
- 🎯 初心者ではなく、Javaなどの言語を1つ以上知っている開発者向けに、Go言語を迅速に理解し実践する方法を提案している。
- 📚 書籍を読まずに、コードを書くことから始めることで効率的に学習することができると主張している。
- 🛠️ Go言語ではプロジェクトの設定が簡単で、すぐに開発を開始することができると紹介されている。
- 🔧 単体テストの重要性を強調し、Go言語におけるテストアサーションの欠如と、testifyパッケージを使用してテストを行う方法を説明している。
- 🌐 ウェブサーバーの作成、ルーティング、人気のあるライブラリの使い方、JSONの扱い方、データモデルの作り方などのバックエンド開発に必要な分野に焦点を当てている。
- 📈 ORM(Object-Relational Mapping)フレームワークの代わりに、シンプルなアプローチとしてデータベースとの直接的なやり取りを推奨している。
- 🔗 Go言語における依存関係の扱い方、特にハンドラー内でデータベース接続などの依存関係をどう扱うかについて、シンプルなアプローチからより高度な方法までを紹介している。
- 🔄 HTTPリクエストの送信方法についても触れており、Go言語での外部APIとの連携方法を学ぶことができる。
- 🐳 Dockerコンテナを使用してローカル開発環境を構築し、テストコンテナを使用してデータベースを管理する方法も提案されている。
- 📘 スクリプトの最後には、Go言語をさらに学ぶ予定がある旨を述べており、今後も同様のビデオを期待することができると示唆している。
Q & A
最近ではどういった理由でGo言語を学び始めましたか?
-最近、ソフトウェア開発の経験があるが、新しいプログラミング言語を学び始めることに興味を持ったためです。特にGo言語は、既にJavaなどの言語を知っている経験豊富な開発者にとって、新しい言語を学ぶのに適しています。
Go言語のプロジェクト設定はなぜ簡単だと言えますか?
-Go言語のプロジェクト設定は、Goをインストールし、フォルダーを作成して`go mod`を実行するだけで始められるため、非常に簡単です。これにより、追加のライブラリをインポートし、メイン関数を実行できるようになり、素早く開発を進めることができます。
Go言語でテストを書く際に使用するassertionライブラリは何ですか?
-Go言語のビルトインテストアサーションは存在しないため、外部のライブラリである`testify`の`assert`パッケージを使用して、テストアサーションを行えます。これにより、`Equal`などのアサーション機能が利用できます。
Go言語でWebサーバーを作成する際に、どのようなライブラリが人気ですか?
-Go言語でWebサーバーを作成する際には、Gin、Echo、Fiber、Chi、gorillaなどが人気です。しかし、軽量なライブラリを好む開発者にとっては、Chiが人気で、ルートにミドルウェアを登録するオプションを持つルータです。
Go言語でJSONを扱う際には、標準パッケージの他にどのようなライブラリがありますか?
-Go言語でJSONを扱う際には、標準パッケージである`encoding/json`がありますが、Chiライブラリの`render`パッケージも利用できます。これは、レスポンスの正しいヘッダーを設定するのに役立ちます。
Go言語でSQLデータベースと連携する際のORMフレームワークとは何ですか?
-Go言語でSQLデータベースと連携する際、ORMフレームワークとして有名なのはGORMです。しかし、ORMを使わずにシンプルにデータベースとの連携を行う場合、Jetというライブラリがあります。これはスキーマからGoの構造体を生成し、型安全なSQLクエリを書くことができます。
Go言語で依存性をハンドラーに渡す際のシンプルなアプローチとは何ですか?
-Go言語で依存性をハンドラーに渡す際のシンプルなアプローチは、グローバル変数を使用することであり、データベース接続ハンドラなどへのアクセスが必要な場合に使われます。これはシンプルなアプリケーションには適していますが、依存性が増大する場合は他の方法が推奨されます。
Go言語で外部APIにHTTPリクエストを送信する際のアプローチはどのようなものですか?
-Go言語で外部APIにHTTPリクエストを送信する際は、標準パッケージである`net/http`を使用してプロキシハンドラーを作成し、リクエストを実行します。これにより、他の言語と同様に外部APIと連携することができます。
Dockerコンテナを使用してローカルデータベースを実行する際の利点は何ですか?
-Dockerコンテナを使用してローカルデータベースを実行する際の利点は、データベースを簡単に起動し、アプリケーションコードからDockerイメージを制御できることです。また、テストコンテナを使用することで、テスト時にデータベースの初期移行を適用し、ローカル開発と試行錯誤が容易になります。
Go言語の学習を進める際、今後の計画は何ですか?
-Go言語の学習を進める予定で、今後もより真剣に学び、チャンネルでさらに多くのビデオを投稿する予定です。興味を持っていただけると嬉しいので、チャンネルを購読してコメントやlikesをしていただけると助かります。
Go言語の学習にあたって、どのようなリソースを利用していますか?
-Go言語の学習にあたっては、公式ドキュメントやオンラインの記事、コミュニティのサポートなどを利用しています。また、自分のブログにも学習手順やリンクを掲載し、テキストバージョンを提供しています。
Go言語でWebサービスを開発する際に、データベースのスキーマを定義し、モデルを作成するプロセスはどのように行われますか?
-Go言語でWebサービスを開発する際に、データベースのスキーマを定義し、モデルを作成するためには、Jetライブラリを使用してスキーマを定義し、ジェネレータを実行することで、Goの構造体としてテーブル構造を生成します。これにより、型安全なSQLクエリを書くことができます。
Outlines
😀 新しい言語Goの学習アプローチ
この段落では、Go言語を学ぶ方法について紹介しています。ソフトウェア開発の経験がある人を対象に、Go言語の基本的なプロジェクト設定から始め、テストの書き方、そしてウェブサーバーの作成方法などを説明しています。Go言語の特徴であるシンプルなプロジェクト設定や、標準的なテストライブラリであるtestifyパッケージの使い方についても触れています。
🛠️ Go言語での開発ツールとライブラリの使い方
第二段落では、Go言語におけるウェブフレームワークの調査方法や、JSONデータの扱い方、データベースとの連携について詳しく説明しています。特に、軽量なライブラリであるchiを選び、ウェブサーバーの作成例を紹介しています。また、Go言語の標準パッケージであるencoding/jsonを使用して、構造体をJSONにシリアル化する例も紹介されています。
🔧 Go言語でのデータベース操作とHTTPリクエスト
第三段落では、Go言語でSQLを使用したデータベース操作について紹介しています。ORMの代わりに、シンプルなアプローチとしてjetというライブラリを使用して、データベースとのやり取りを行う方法を説明しています。さらに、Go言語でのHTTPリクエストの例として、プロキシハンドラーを作成し外部APIへのリクエストを行う方法も紹介されています。
📚 Go言語における依存性の管理とDockerの活用
第四段落では、Go言語での依存性の管理方法について議論しています。グローバル変数を使用するシンプルな方法から、依存性注入やクロージャを使用する方法までを紹介しています。また、Dockerコンテナを使用してローカル開発環境を構築する方法についても説明しており、testcontainers-goを使用してデータベースを管理する例も紹介されています。
🎉 Go言語の学習を通じて得られる成果と今後の展望
最後の段落では、Go言語を学ぶことで得られる成果と、今後の学習計画について話しています。チャンネルに登録して今後のビデオも楽しみにすると言っています。また、コメントやいいねをすることで、作者にとって大きな励みになるという感謝の言葉も添えています。
Mindmap
Keywords
💡Go言語
💡ソフトウェア開発
💡プロジェクト設定
💡テスト
💡Webサーバー
💡JSON
💡構造体
💡SQL
💡依存性注入
💡HTTPリクエスト
Highlights
学習Go言語のアプローチを共有し、経験豊富な開発者向けに焦点を当てた。
Go言語のプロジェクト設定は非常に簡単。
最初のコードをできるだけ早く書くことが主要目標。
Go言語では、テストの書き方とテストアサーションライブラリ「testify」を使用する方法を紹介。
エディターの設定とユニットテストの重要性を強調。
バックエンド開発におけるマイクロサービス作成の方法を学ぶ。
JSONの扱い方とGo言語におけるデータモデル化の方法を解説。
リレーショナルデータベースとの連携方法とSQLを使用する理由を説明。
ウェブサーバーを作成する際のライブラリ選定と「chi」ライブラリの紹介。
Go言語でHTTPリクエストを扱う方法と「net/http」パッケージの使用。
構造体からJSONへの変換方法と「encoding/json」パッケージの活用。
「chi」ライブラリの「render」パッケージを使ったレスポンスの正しい形式化。
ORMの代わりにシンプルなアプローチでデータベースと連携する理由と「gojet」ライブラリの紹介。
データベースへの依存関係の扱いとグローバル変数を使用するシンプルなアプローチ。
Go言語でのHTTPリクエストのプロキシ機能の例を紹介。
Dockerコンテナを使用してローカルデータベースを管理する「testcontainers-go」ライブラリの紹介。
Go言語をより真剣に学ぶ予定と今後のビデオの期待を呼びかけ。
Transcripts
hello there so recently I decided to
learn go language and uh in this video I
just want to share my approach to
learning a new language uh it won't work
probably if you are a beginner in in
software development and uh go is your
first language I'll mostly focus on
experienced developers if you're
familiar with uh common approaches in
software and you know one or two
languages uh something like Java or
something commonly used I think this is
a nice uh way uh to quickly understand
uh a lot about the language about the
ecosystem uh and uh to gain some
hands-on experience as well so all that
I'm going to um talk about are written
in my uh blog as well so if you prefer a
text version uh the link will be in the
description so my Approach is uh not
wasting too much time uh not trying to
get all this structured knowledge uh
don't read any books um as you just
starting uh the main uh goal is to um
write your first code as fast as
possible and then uh create a workflow
so you can iterate and try new things uh
really fast so in go it's really easy
because the uh project setup is really
simple you just install go and then you
create a folder and run go mode in it
inside and basically you're good to go
from that you can import additional
libraries you can run and execute your
main function and um after that you
probably can just play with a main
function uh for for a while put some
code there try some things uh but
eventually uh the main function uh could
be tricky to use because you always need
to change things inside so what I
suggest you uh becoming really
comfortable with is uh writing some
simple tests and uh let's take a look
how you can do that so for example we
create a new function um let me delete
this so let's say we want some simple
functioning go uh we do something like
Funk and let's call it
some uh
some numbers right and then we have an
INT and then an INT as
well want to return an
INT return and we just do some of that
right so instead of calling this
function from Main you probably want to
create a underscore test file and then
you basically have access here to test
what you want uh but first before we
start that's the first uh finding that I
made in go is that um the built-in test
assertions basically nonexist so as as I
got it uh so but there is a function
called Library called testify a pack
package that we're going to use and you
can do it by calling assert and you now
have access to things like equal uh and
then we put T here let me delete
everything um like this and here
basically you have an expected value
let's say five and then we can call our
function here so let's do something
three
and three uh and to validate we can run
test which should fail now right yeah so
now we have a failing test and we can we
now have an easy way to iterate and try
new things so that's the main uh Point
number one uh set up your editor uh set
up the testing uh the unit test so you
can quickly validate that functions that
you're writing are doing what you expect
cool but after doing this basic setup uh
I suggest we dive straight into
um developing a service right and um
these areas are most commonly used in
any language in if you're doing some
sort of backend Development building
microservices or whatever and those
areas are basically how you create a web
server uh how you create Roots uh what
libraries are popular and uh useful in
this area for the uh for the routa or
some kind of web
Frameworks um you need to investigate
what's popular in the language in the in
the
ecosystem once you done that um the the
other part is how to work with Json
because basically Json is the um main
format nowadays for the HTTP requests of
course if you're not doing the grpc
approach uh but yeah um usually really
interested how you work with Jon in
language just to understand how you
serialize deserialize data Etc and also
in this area uh you will learn how you
model your data right so in go we have
structs uh and in other languages like
classes Etc cool so Json and then you
combine these two so you now have an API
that returns a Json uh format
and after that uh the final bit is to
add persistence layer so we want to to
introduce some kind of relational
database and I highly suggest starting
with uh SQL relational database because
it's from my experience still the most
popular option and if you understand how
that works um you'll cover most of the
cases and if you need a non- relational
database no SQL uh you can do that later
so let's start with um with the web
server uh I made a bit of research and
there are a couple libraries available
yeah so we have gin Echo fiber Chi
gorilla uh but I wasn't looking for you
know like a big framework I wanted U and
also coming from closure background uh I
just prefer lightweight libraries for
things and after some research I think
chii is kind of popular option and it's
also really lightweight because it's
literally just a routa with option to
register middleware on separate sets of
roots so I picked this one uh but before
that you can probably just start
creating a web server with uh only uh
builtin like core language packages and
there is like a net HTTP and with net
HTP you can liter create a web server
with like couple lines like this so
basically you register your Handler and
then the root and you can configure your
uh application to listen on specific
Port but let's jump straight into uh chi
and uh this is their main uh main uh
example how to get started and we can
see here we're creating a new router
then we register the log
middleware uh and basically after that
we able to create new Roots as we go so
we have like a just slash here but you
can add more Roots as you go
so just couple lines and after that you
can just run your main function with go
run Dot and you will have a working web
server right
so let's go here I have a bit more code
already uh but really the main here is
this in the main function uh we're just
creating a new rout and register some
Roots here
cool after that we have uh Json as I
said and um here we have two options so
one is uh standard package called
encoring Json and with that you can um
convert your structs into and serialize
them into Json uh um let's try that so I
created like a user struct which has ID
and name and for Json we can use this uh
tags that basically will tell that when
we serialize it to Json this uh Capital
case ID will be converted into lowercase
ID and name uh started with a capital
letter will be converted to a new to a
name or lowercase but you you can do
whatever you want you can do like a
first underscore name here this will all
work but let's keep it name and here we
have our test function really handy
because now we can do something like
user and um let's say it will be uh user
and we create it with
ID
one and
name let's say
Andre and after that we want use json.
Marshall and then we pass the user
inside so result of that will be a bite
rate so bite array so let's say
uh user byes or it could be an error
which we can ignore for now and after
that let's create an assertion so we
want to assert
equals and here we have test then we
then we will um do something that
expected and here we just convert our
bite rate into a
string and pass our
user um user bites
inside all right let's run our test so
go test it should
fail and we see
that we're expecting this string and we
can do it here so let's
do slash
um
ID it will
be one and
then
name should
[Music]
be
Andre let's run that again
and now we're back to passing test so
here you how you
convert struct to
Json but as we uh picked Chi uh as the
library there is a uh render package
from Chi as well and we can use that to
help us with uh correct headers in in
the responses Etc so it works like like
like this so basically we have here a
get user Json endpoint and um um
creating the the user uh and
initializing U the struct and then we
call the
render uh and we pass status and the
render and this will work only if you uh
extend your struct and add a function
that implements this
render um Rand the function so once you
done that uh you can literally just call
it like this and it will be correctly
rendered into adjon so let's go here and
let's rename so as you can see here
we're defining some
uh URLs uh some roots and uh SL user
will be calling this get user Json
endpoint uh we can now run our
application let's go to SL user it enter
and you see we have our correct response
so the next big one as I said is working
with uh
SQL and uh I don't really like orms
probably due to my um early days in
software development when I had to work
with Java and
hibernate um and but basically seriously
uh
orms uh have its own Niche I think but
my problem is that sometimes when you
start building something your
application is too simple to use orm but
after it grows usually there is a point
when your application becomes too
complex for ANM so it's not perfectly
matching what omm provides and in that
case you try to work around the orms you
try to optimize the queries that are
generated that you cannot fully control
so I prefer simpler approach uh just
work with a database and write plain
queries the thing that I enjoy is some
kind of query Builder and it depends on
the language so um when I was working
with Java I was trying to move from
hibernate to a library called Juke which
was uh a library that allowed you to
write um a type save uh SQL queries and
it worked like uh you use that generator
from the library that was going into
your schema in your database and it was
generating Java classes based on that
schema and after that you have all out
of completion inside your queries and
here I was looking for the similar uh
option and I found like almost exactly
the same approach and library is called
uh jet in the GitHub gojet right
here and they provide
an um an example for the schema so here
we have a a file uh which is like
a a creation of the database and then it
will be like filling the data for us so
I use that as an example and inside our
um application I was running something
like Jet and then this command so
basically we're defining schema and pass
uh that will be the pass of the output
and then our local database uh once you
run that you'll have um you'll have a
new folder do gen with a database models
and go files right here so it's really
simple the main one is like model I
think and if you go to film for example
you see that our table structure is
defined as a struct in
goang um so what we can do after that it
basically we can write our
statements um like this so you have like
select from the library and then from
you use the film table then we can
create some expressions like where film
length is greater than this value and
order by Etc basically it provides an a
completion for you so for example
instead of all columns let's see what's
available so let's say description and
now in our query we just select the
description right here let's change it
to all
columns so that's how you build a query
after you created the query we want to
execute it basically it's just uh like
this so we have um our output struct
where we want to put the result we just
want a model film there uh this is the
destination and then we do query on our
database object and it will fill these
films as a result
and yeah it's a bit of junk here but at
the end we still using this render uh
and we rendering the film response uh
struct with a film inside just a single
one and we'll return that so let's
actually um change it to let's say let's
call it just like a
film film it's not really a rest API but
yeah it should work for now
let's restart the server and let's try
this SL film endpoint we were calling
user but now let's try
slashfilm and here we have some response
from the back end and it's actually one
of the films from the
database um one other thing I wanted to
discuss is and that's one of the areas I
usually interested is uh how you pass
your dependencies inside your Cod inside
handlers because usually you want access
to at least the database uh connection
database Handler and here I was
following the most simple approach
basically just put it as a global war uh
and for simple application that works
for more complex things you when you
have more
dependencies um that won't work but what
I want to share is a really nice article
right here uh about organizing database
accessing goal and it basically starts
from the the easy one with a global war
but then we have uh things like um
dependency injections here uh and uh the
closures we are closures and um also
there's a way to pass it as a
context uh in the handlers but it's not
really recommended as I understand in
the request context because it should be
just um things related to the
request uh so if you're interested this
really nice article again you can find
the link here in my blog post if you
want and yeah finally um I looked into
making HTTP
requests and I created a new Handler
with a proxy and inside the proxy I'm
just calling some random API available
in on the internet uh and checking how
it's it's working in language how you do
outgoing HTTP
requests um and right at the end um I
just want to mention that I wasn't
running my local database as a Docker uh
and basically it was running in a Docker
container uh I wasn't just manually
starting it because uh in most cases I
just prefer to use test containers and
it basically allows you to control your
Docker images from your uh application
code and usually it's used in tests by
the name of test containers basically
for the test uh but here I just uh used
it in a hacky way and I just use this
cord inside main function before I
running the um web server to start the
database and when I run man main it
basically goes and talks to Docker uh
agent and it spins the container that I
need
and here I'm just providing the names uh
DB name username and here I'm putting
this uh in it script which I took from
jet and you basically just put it here
and on the start it will apply this
initial uh initial migration so once the
database is running you have all the
tables inside and all the data which is
really handy for quick local development
and trying things so yeah highly
recommend uh test containers go is
supported as one of the languages and a
lot of containers are supported as well
so it's really cool if you're on
database if you're on Kafka redis
whatever you're interested to try it's
um probably the most easy way to to get
started cool so hope you enjoyed my uh
brief introduction and I'm planning to
uh learn go more seriously probably so
expect more videos on the channel if you
interested in that please subscribe and
also it really helps when you comment
and like the video so that really helps
all the links will be in the description
so check it out and see you next video
bye-bye
5.0 / 5 (0 votes)