My approach to learning a new language as an experienced developer (Go)

Andrey Fadeev
14 May 202421:42

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

00:00

😀 新しい言語Goの学習アプローチ

この段落では、Go言語を学ぶ方法について紹介しています。ソフトウェア開発の経験がある人を対象に、Go言語の基本的なプロジェクト設定から始め、テストの書き方、そしてウェブサーバーの作成方法などを説明しています。Go言語の特徴であるシンプルなプロジェクト設定や、標準的なテストライブラリであるtestifyパッケージの使い方についても触れています。

05:01

🛠️ Go言語での開発ツールとライブラリの使い方

第二段落では、Go言語におけるウェブフレームワークの調査方法や、JSONデータの扱い方、データベースとの連携について詳しく説明しています。特に、軽量なライブラリであるchiを選び、ウェブサーバーの作成例を紹介しています。また、Go言語の標準パッケージであるencoding/jsonを使用して、構造体をJSONにシリアル化する例も紹介されています。

10:03

🔧 Go言語でのデータベース操作とHTTPリクエスト

第三段落では、Go言語でSQLを使用したデータベース操作について紹介しています。ORMの代わりに、シンプルなアプローチとしてjetというライブラリを使用して、データベースとのやり取りを行う方法を説明しています。さらに、Go言語でのHTTPリクエストの例として、プロキシハンドラーを作成し外部APIへのリクエストを行う方法も紹介されています。

15:05

📚 Go言語における依存性の管理とDockerの活用

第四段落では、Go言語での依存性の管理方法について議論しています。グローバル変数を使用するシンプルな方法から、依存性注入やクロージャを使用する方法までを紹介しています。また、Dockerコンテナを使用してローカル開発環境を構築する方法についても説明しており、testcontainers-goを使用してデータベースを管理する例も紹介されています。

20:06

🎉 Go言語の学習を通じて得られる成果と今後の展望

最後の段落では、Go言語を学ぶことで得られる成果と、今後の学習計画について話しています。チャンネルに登録して今後のビデオも楽しみにすると言っています。また、コメントやいいねをすることで、作者にとって大きな励みになるという感謝の言葉も添えています。

Mindmap

Keywords

💡Go言語

Go言語は、Googleによって開発されたプログラミング言語で、並行性やチャンネルを備えたシンプルで高速な実行が可能であることが特徴です。このビデオでは、Go言語を学ぶ方法を紹介しており、他の言語に比べてGo言語のプロジェクト設定が簡単であることや、標準パッケージを利用してWebサーバーを簡単に作成することができるという利点を強調しています。

💡ソフトウェア開発

ソフトウェア開発とは、ソフトウェアを設計、開発、テストし、保守するプロセスを指します。ビデオでは、ソフトウェア開発の経験がある開発者を対象にGo言語を学ぶアプローチを紹介しており、Javaなどの他の言語を知っている開発者にとっては、Go言語を迅速に習得することができると示唆しています。

💡プロジェクト設定

プロジェクト設定とは、ソフトウェア開発において新しいプロジェクトを開始する際の初期設定を意味します。ビデオではGo言語のプロジェクト設定が非常に簡単であると触れており、Go言語をインストールし、フォルダを作成して`go mod init`を実行するだけで始められると説明しています。

💡テスト

ソフトウェア開発においてテストとは、開発したコードが期待通りに動作することを検証するプロセスです。ビデオではGo言語でテストを作成し、関数が正しく動作することを迅速に検証する重要性を強調しています。また、Go言語の標準的なテストアサーションが存在しないことと、代わりに`testify`パッケージを使用する例を説明しています。

💡Webサーバー

Webサーバーとは、クライアントからのリクエストに応じてWebページを提供するソフトウェアアプリケーションです。ビデオではGo言語でWebサーバーを作成し、`net/http`パッケージを使用して基本的なサーバーを立ち上げ、その後`chi`という軽量なライブラリを使用してより高度な機能を実装することができると紹介しています。

💡JSON

JSONは、データ交換を目的とした軽量のデータフォーマットです。ビデオではGo言語でJSONを扱う方法について触れており、`encoding/json`パッケージを使用してGo言語の構造体をJSONにシリアル化することができると説明しています。また、JSONデータを正しく扱うために`chi`ライブラリの`render`パッケージを使用する例も紹介されています。

💡構造体

構造体は、プログラミングにおいて複数の値を集約して一つの単位として扱うデータ型です。ビデオではGo言語における構造体を使用してデータをモデル化し、JSONと組み合わせてAPIがどのように動作するかを説明しています。例えば、`User`構造体を使用してユーザーデータを表現し、それをJSONにシリアル化する場面があります。

💡SQL

SQLは、リレーショナルデータベース管理システムと通信するための言語です。ビデオではGo言語でSQLを使用してデータベースとのやり取りを行う方法を紹介しており、`database/sql`パッケージと`go-jet`というORMライブラリを使用してデータベースとのインタラクションをシンプルに行う方法を説明しています。

💡依存性注入

依存性注入は、ソフトウェア開発におけるデザインパターンの一種で、オブジェクトが必要なリソースを直接作成するのではなく、それらを提供される形で実装します。ビデオではGo言語での依存性注入について触れており、シンプルなグローバル変数を使用する一方で、より複雑なアプリケーションでは依存性注入が推奨されると説明しています。

💡HTTPリクエスト

HTTPリクエストとは、ウェブサーバーにデータを要求するプロセスを指します。ビデオではGo言語でHTTPリクエストを作成し、他のウェブサービスと通信する方法を紹介しています。プロキシ機能を実装し、外部APIを呼び出す方法を学ぶことが、Go言語での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

play00:00

hello there so recently I decided to

play00:04

learn go language and uh in this video I

play00:08

just want to share my approach to

play00:11

learning a new language uh it won't work

play00:14

probably if you are a beginner in in

play00:17

software development and uh go is your

play00:19

first language I'll mostly focus on

play00:22

experienced developers if you're

play00:24

familiar with uh common approaches in

play00:27

software and you know one or two

play00:30

languages uh something like Java or

play00:32

something commonly used I think this is

play00:35

a nice uh way uh to quickly understand

play00:39

uh a lot about the language about the

play00:42

ecosystem uh and uh to gain some

play00:46

hands-on experience as well so all that

play00:50

I'm going to um talk about are written

play00:53

in my uh blog as well so if you prefer a

play00:57

text version uh the link will be in the

play01:00

description so my Approach is uh not

play01:03

wasting too much time uh not trying to

play01:06

get all this structured knowledge uh

play01:09

don't read any books um as you just

play01:12

starting uh the main uh goal is to um

play01:18

write your first code as fast as

play01:20

possible and then uh create a workflow

play01:23

so you can iterate and try new things uh

play01:26

really fast so in go it's really easy

play01:30

because the uh project setup is really

play01:34

simple you just install go and then you

play01:36

create a folder and run go mode in it

play01:39

inside and basically you're good to go

play01:42

from that you can import additional

play01:45

libraries you can run and execute your

play01:48

main function and um after that you

play01:52

probably can just play with a main

play01:54

function uh for for a while put some

play01:57

code there try some things uh but

play02:02

eventually uh the main function uh could

play02:05

be tricky to use because you always need

play02:07

to change things inside so what I

play02:10

suggest you uh becoming really

play02:14

comfortable with is uh writing some

play02:16

simple tests and uh let's take a look

play02:20

how you can do that so for example we

play02:22

create a new function um let me delete

play02:25

this so let's say we want some simple

play02:27

functioning go uh we do something like

play02:30

Funk and let's call it

play02:33

some uh

play02:35

some numbers right and then we have an

play02:40

INT and then an INT as

play02:43

well want to return an

play02:47

INT return and we just do some of that

play02:52

right so instead of calling this

play02:55

function from Main you probably want to

play02:57

create a underscore test file and then

play03:00

you basically have access here to test

play03:02

what you want uh but first before we

play03:05

start that's the first uh finding that I

play03:08

made in go is that um the built-in test

play03:12

assertions basically nonexist so as as I

play03:15

got it uh so but there is a function

play03:19

called Library called testify a pack

play03:22

package that we're going to use and you

play03:23

can do it by calling assert and you now

play03:27

have access to things like equal uh and

play03:31

then we put T here let me delete

play03:34

everything um like this and here

play03:38

basically you have an expected value

play03:40

let's say five and then we can call our

play03:44

function here so let's do something

play03:47

three

play03:49

and three uh and to validate we can run

play03:53

test which should fail now right yeah so

play03:58

now we have a failing test and we can we

play04:01

now have an easy way to iterate and try

play04:03

new things so that's the main uh Point

play04:07

number one uh set up your editor uh set

play04:11

up the testing uh the unit test so you

play04:15

can quickly validate that functions that

play04:17

you're writing are doing what you expect

play04:21

cool but after doing this basic setup uh

play04:25

I suggest we dive straight into

play04:30

um developing a service right and um

play04:33

these areas are most commonly used in

play04:37

any language in if you're doing some

play04:39

sort of backend Development building

play04:42

microservices or whatever and those

play04:46

areas are basically how you create a web

play04:48

server uh how you create Roots uh what

play04:53

libraries are popular and uh useful in

play04:56

this area for the uh for the routa or

play05:01

some kind of web

play05:03

Frameworks um you need to investigate

play05:06

what's popular in the language in the in

play05:08

the

play05:09

ecosystem once you done that um the the

play05:13

other part is how to work with Json

play05:15

because basically Json is the um main

play05:20

format nowadays for the HTTP requests of

play05:24

course if you're not doing the grpc

play05:27

approach uh but yeah um usually really

play05:30

interested how you work with Jon in

play05:33

language just to understand how you

play05:34

serialize deserialize data Etc and also

play05:38

in this area uh you will learn how you

play05:41

model your data right so in go we have

play05:44

structs uh and in other languages like

play05:47

classes Etc cool so Json and then you

play05:51

combine these two so you now have an API

play05:56

that returns a Json uh format

play06:00

and after that uh the final bit is to

play06:04

add persistence layer so we want to to

play06:08

introduce some kind of relational

play06:11

database and I highly suggest starting

play06:12

with uh SQL relational database because

play06:15

it's from my experience still the most

play06:18

popular option and if you understand how

play06:21

that works um you'll cover most of the

play06:24

cases and if you need a non- relational

play06:27

database no SQL uh you can do that later

play06:30

so let's start with um with the web

play06:34

server uh I made a bit of research and

play06:37

there are a couple libraries available

play06:40

yeah so we have gin Echo fiber Chi

play06:45

gorilla uh but I wasn't looking for you

play06:48

know like a big framework I wanted U and

play06:51

also coming from closure background uh I

play06:55

just prefer lightweight libraries for

play06:58

things and after some research I think

play07:01

chii is kind of popular option and it's

play07:03

also really lightweight because it's

play07:05

literally just a routa with option to

play07:09

register middleware on separate sets of

play07:12

roots so I picked this one uh but before

play07:15

that you can probably just start

play07:17

creating a web server with uh only uh

play07:22

builtin like core language packages and

play07:25

there is like a net HTTP and with net

play07:28

HTP you can liter create a web server

play07:31

with like couple lines like this so

play07:33

basically you register your Handler and

play07:35

then the root and you can configure your

play07:39

uh application to listen on specific

play07:41

Port but let's jump straight into uh chi

play07:45

and uh this is their main uh main uh

play07:51

example how to get started and we can

play07:53

see here we're creating a new router

play07:55

then we register the log

play07:57

middleware uh and basically after that

play07:59

we able to create new Roots as we go so

play08:02

we have like a just slash here but you

play08:05

can add more Roots as you go

play08:08

so just couple lines and after that you

play08:11

can just run your main function with go

play08:14

run Dot and you will have a working web

play08:19

server right

play08:22

so let's go here I have a bit more code

play08:25

already uh but really the main here is

play08:29

this in the main function uh we're just

play08:32

creating a new rout and register some

play08:35

Roots here

play08:37

cool after that we have uh Json as I

play08:42

said and um here we have two options so

play08:46

one is uh standard package called

play08:49

encoring Json and with that you can um

play08:54

convert your structs into and serialize

play08:57

them into Json uh um let's try that so I

play09:01

created like a user struct which has ID

play09:04

and name and for Json we can use this uh

play09:08

tags that basically will tell that when

play09:11

we serialize it to Json this uh Capital

play09:14

case ID will be converted into lowercase

play09:17

ID and name uh started with a capital

play09:20

letter will be converted to a new to a

play09:22

name or lowercase but you you can do

play09:24

whatever you want you can do like a

play09:25

first underscore name here this will all

play09:29

work but let's keep it name and here we

play09:33

have our test function really handy

play09:37

because now we can do something like

play09:39

user and um let's say it will be uh user

play09:45

and we create it with

play09:47

ID

play09:50

one and

play09:54

name let's say

play09:57

Andre and after that we want use json.

play10:01

Marshall and then we pass the user

play10:03

inside so result of that will be a bite

play10:06

rate so bite array so let's say

play10:09

uh user byes or it could be an error

play10:14

which we can ignore for now and after

play10:17

that let's create an assertion so we

play10:19

want to assert

play10:24

equals and here we have test then we

play10:29

then we will um do something that

play10:33

expected and here we just convert our

play10:35

bite rate into a

play10:38

string and pass our

play10:42

user um user bites

play10:49

inside all right let's run our test so

play10:53

go test it should

play10:54

fail and we see

play10:57

that we're expecting this string and we

play11:00

can do it here so let's

play11:03

do slash

play11:06

um

play11:08

ID it will

play11:10

be one and

play11:15

then

play11:18

name should

play11:20

[Music]

play11:21

be

play11:25

Andre let's run that again

play11:32

and now we're back to passing test so

play11:34

here you how you

play11:36

convert struct to

play11:38

Json but as we uh picked Chi uh as the

play11:43

library there is a uh render package

play11:47

from Chi as well and we can use that to

play11:51

help us with uh correct headers in in

play11:56

the responses Etc so it works like like

play11:59

like this so basically we have here a

play12:02

get user Json endpoint and um um

play12:06

creating the the user uh and

play12:09

initializing U the struct and then we

play12:13

call the

play12:14

render uh and we pass status and the

play12:17

render and this will work only if you uh

play12:22

extend your struct and add a function

play12:25

that implements this

play12:27

render um Rand the function so once you

play12:31

done that uh you can literally just call

play12:34

it like this and it will be correctly

play12:37

rendered into adjon so let's go here and

play12:41

let's rename so as you can see here

play12:43

we're defining some

play12:45

uh URLs uh some roots and uh SL user

play12:50

will be calling this get user Json

play12:53

endpoint uh we can now run our

play12:56

application let's go to SL user it enter

play13:01

and you see we have our correct response

play13:04

so the next big one as I said is working

play13:07

with uh

play13:09

SQL and uh I don't really like orms

play13:14

probably due to my um early days in

play13:17

software development when I had to work

play13:19

with Java and

play13:21

hibernate um and but basically seriously

play13:24

uh

play13:25

orms uh have its own Niche I think but

play13:29

my problem is that sometimes when you

play13:32

start building something your

play13:33

application is too simple to use orm but

play13:38

after it grows usually there is a point

play13:40

when your application becomes too

play13:43

complex for ANM so it's not perfectly

play13:46

matching what omm provides and in that

play13:49

case you try to work around the orms you

play13:52

try to optimize the queries that are

play13:54

generated that you cannot fully control

play13:57

so I prefer simpler approach uh just

play14:01

work with a database and write plain

play14:04

queries the thing that I enjoy is some

play14:07

kind of query Builder and it depends on

play14:10

the language so um when I was working

play14:13

with Java I was trying to move from

play14:15

hibernate to a library called Juke which

play14:19

was uh a library that allowed you to

play14:21

write um a type save uh SQL queries and

play14:26

it worked like uh you use that generator

play14:29

from the library that was going into

play14:31

your schema in your database and it was

play14:34

generating Java classes based on that

play14:36

schema and after that you have all out

play14:38

of completion inside your queries and

play14:41

here I was looking for the similar uh

play14:43

option and I found like almost exactly

play14:46

the same approach and library is called

play14:48

uh jet in the GitHub gojet right

play14:52

here and they provide

play14:55

an um an example for the schema so here

play15:00

we have a a file uh which is like

play15:05

a a creation of the database and then it

play15:08

will be like filling the data for us so

play15:11

I use that as an example and inside our

play15:16

um application I was running something

play15:19

like Jet and then this command so

play15:22

basically we're defining schema and pass

play15:26

uh that will be the pass of the output

play15:28

and then our local database uh once you

play15:32

run that you'll have um you'll have a

play15:36

new folder do gen with a database models

play15:40

and go files right here so it's really

play15:43

simple the main one is like model I

play15:46

think and if you go to film for example

play15:48

you see that our table structure is

play15:51

defined as a struct in

play15:54

goang um so what we can do after that it

play15:57

basically we can write our

play15:59

statements um like this so you have like

play16:03

select from the library and then from

play16:07

you use the film table then we can

play16:09

create some expressions like where film

play16:13

length is greater than this value and

play16:16

order by Etc basically it provides an a

play16:20

completion for you so for example

play16:22

instead of all columns let's see what's

play16:24

available so let's say description and

play16:27

now in our query we just select the

play16:29

description right here let's change it

play16:32

to all

play16:33

columns so that's how you build a query

play16:36

after you created the query we want to

play16:38

execute it basically it's just uh like

play16:41

this so we have um our output struct

play16:46

where we want to put the result we just

play16:48

want a model film there uh this is the

play16:51

destination and then we do query on our

play16:53

database object and it will fill these

play16:57

films as a result

play17:00

and yeah it's a bit of junk here but at

play17:02

the end we still using this render uh

play17:05

and we rendering the film response uh

play17:09

struct with a film inside just a single

play17:12

one and we'll return that so let's

play17:14

actually um change it to let's say let's

play17:18

call it just like a

play17:20

film film it's not really a rest API but

play17:24

yeah it should work for now

play17:29

let's restart the server and let's try

play17:31

this SL film endpoint we were calling

play17:34

user but now let's try

play17:36

slashfilm and here we have some response

play17:40

from the back end and it's actually one

play17:42

of the films from the

play17:44

database um one other thing I wanted to

play17:47

discuss is and that's one of the areas I

play17:50

usually interested is uh how you pass

play17:54

your dependencies inside your Cod inside

play17:57

handlers because usually you want access

play17:59

to at least the database uh connection

play18:03

database Handler and here I was

play18:05

following the most simple approach

play18:07

basically just put it as a global war uh

play18:10

and for simple application that works

play18:12

for more complex things you when you

play18:14

have more

play18:15

dependencies um that won't work but what

play18:19

I want to share is a really nice article

play18:23

right here uh about organizing database

play18:26

accessing goal and it basically starts

play18:28

from the the easy one with a global war

play18:30

but then we have uh things like um

play18:35

dependency injections here uh and uh the

play18:39

closures we are closures and um also

play18:45

there's a way to pass it as a

play18:48

context uh in the handlers but it's not

play18:50

really recommended as I understand in

play18:53

the request context because it should be

play18:55

just um things related to the

play18:59

request uh so if you're interested this

play19:02

really nice article again you can find

play19:05

the link here in my blog post if you

play19:08

want and yeah finally um I looked into

play19:13

making HTTP

play19:15

requests and I created a new Handler

play19:18

with a proxy and inside the proxy I'm

play19:20

just calling some random API available

play19:23

in on the internet uh and checking how

play19:27

it's it's working in language how you do

play19:30

outgoing HTTP

play19:31

requests um and right at the end um I

play19:38

just want to mention that I wasn't

play19:40

running my local database as a Docker uh

play19:44

and basically it was running in a Docker

play19:47

container uh I wasn't just manually

play19:50

starting it because uh in most cases I

play19:53

just prefer to use test containers and

play19:55

it basically allows you to control your

play19:57

Docker images from your uh application

play20:01

code and usually it's used in tests by

play20:04

the name of test containers basically

play20:06

for the test uh but here I just uh used

play20:10

it in a hacky way and I just use this

play20:12

cord inside main function before I

play20:14

running the um web server to start the

play20:20

database and when I run man main it

play20:22

basically goes and talks to Docker uh

play20:26

agent and it spins the container that I

play20:28

need

play20:29

and here I'm just providing the names uh

play20:31

DB name username and here I'm putting

play20:35

this uh in it script which I took from

play20:37

jet and you basically just put it here

play20:40

and on the start it will apply this

play20:43

initial uh initial migration so once the

play20:47

database is running you have all the

play20:49

tables inside and all the data which is

play20:51

really handy for quick local development

play20:54

and trying things so yeah highly

play20:56

recommend uh test containers go is

play20:59

supported as one of the languages and a

play21:02

lot of containers are supported as well

play21:05

so it's really cool if you're on

play21:07

database if you're on Kafka redis

play21:09

whatever you're interested to try it's

play21:12

um probably the most easy way to to get

play21:15

started cool so hope you enjoyed my uh

play21:19

brief introduction and I'm planning to

play21:23

uh learn go more seriously probably so

play21:26

expect more videos on the channel if you

play21:28

interested in that please subscribe and

play21:31

also it really helps when you comment

play21:33

and like the video so that really helps

play21:36

all the links will be in the description

play21:38

so check it out and see you next video

play21:41

bye-bye

Rate This

5.0 / 5 (0 votes)

Related Tags
Go言語学習方法ソフトウェア開発ウェブサーバーJSONSQLテストマイクロサービスDockerHTTPリクエスト
Do you need a summary in English?