first commit
This commit is contained in:
commit
a0a98c9c9f
15
Dockerfile
Normal file
15
Dockerfile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Start with a lightweight Alpine Linux base image
|
||||||
|
FROM golang:alpine
|
||||||
|
|
||||||
|
# Install bash
|
||||||
|
RUN apk add --no-cache bash
|
||||||
|
|
||||||
|
# Set the Current Working Directory inside the container
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy go mod and sum files
|
||||||
|
# COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
|
||||||
|
# RUN go mod download
|
||||||
|
|
73
README.md
Normal file
73
README.md
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
## 開発環境
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker exec -it go_app /bin/bash
|
||||||
|
```
|
||||||
|
|
||||||
|
### goを実行する
|
||||||
|
|
||||||
|
**一時的に実行する(go run)**
|
||||||
|
|
||||||
|
* コンパイルせずに即実行
|
||||||
|
* 一時的なテストや開発中に便利
|
||||||
|
* go run は毎回コンパイルするので、本番運用には向かない
|
||||||
|
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go run main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
* `go run .`
|
||||||
|
* カレントディレクトリ内の main パッケージを実行
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
**コンパイルして実行 (go build)**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go build -o dist/app
|
||||||
|
```
|
||||||
|
|
||||||
|
* go build でバイナリを作成
|
||||||
|
* エントリーポイントとしてmain.go(または package mainのあるファイル)が実行される
|
||||||
|
* ./dist/app で実行できる
|
||||||
|
* 本番環境ではこちらを使う
|
||||||
|
|
||||||
|
## GO言語の概要
|
||||||
|
|
||||||
|
Go(またはGolang)は、Googleによって開発されたオープンソースのプログラミング言語です。
|
||||||
|
シンプルで効率的なコードを書くことを目的としており、特に並行処理やネットワークプログラミングに強みを持っています。
|
||||||
|
|
||||||
|
### 特徴
|
||||||
|
|
||||||
|
- **シンプルな構文**: 学習曲線が緩やかで、読みやすく書きやすいコードが書けます。
|
||||||
|
- **高いパフォーマンス**: コンパイルされたバイナリは高速で、メモリ管理も効率的です。
|
||||||
|
- **並行処理**: Goroutineとチャネルを使用して、簡単に並行処理を実装できます。
|
||||||
|
- **標準ライブラリ**: 豊富な標準ライブラリが提供されており、多くの機能を追加の依存関係なしに利用できます。
|
||||||
|
|
||||||
|
### インストール
|
||||||
|
|
||||||
|
Goのインストール方法については、公式サイトの[インストールガイド](https://golang.org/doc/install)を参照してください。
|
||||||
|
|
||||||
|
### 簡単な例
|
||||||
|
|
||||||
|
以下は、Goで書かれた簡単な「Hello, World!」プログラムです。
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, World!")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 参考リンク
|
||||||
|
|
||||||
|
- [公式サイト](https://golang.org/)
|
||||||
|
- [Goのドキュメント](https://golang.org/doc/)
|
||||||
|
- [Goのパッケージリポジトリ](https://pkg.go.dev/)
|
10
docker-compose.yaml
Normal file
10
docker-compose.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
go-app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
container_name: go_app
|
||||||
|
tty: true
|
||||||
|
volumes:
|
||||||
|
- ./src:/app
|
||||||
|
|
206
docs/go言語.md
Normal file
206
docs/go言語.md
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
|
||||||
|
|
||||||
|
## Go言語の特徴
|
||||||
|
|
||||||
|
* シンプル & 高速
|
||||||
|
* 文法がシンプルで読みやすい
|
||||||
|
* コンパイルが速く、実行時も高速
|
||||||
|
* 静的型付け
|
||||||
|
* int, string, bool などの型を明示(または推論)
|
||||||
|
* 並行処理が得意(goroutine & channel)
|
||||||
|
* 軽量スレッド(goroutine)を使って並行処理が簡単
|
||||||
|
* 単一バイナリでデプロイ
|
||||||
|
* 依存関係を含む1つのバイナリで配布可能
|
||||||
|
* ガベージコレクション(GC)あり
|
||||||
|
* 手動でメモリ管理しなくてもOK
|
||||||
|
* クロスコンパイルが簡単
|
||||||
|
* Windows, Linux, macOS用のバイナリを簡単に作れる
|
||||||
|
* Go言語にclassはない
|
||||||
|
* 構造体(struct)+メソッド+インターフェースでオブジェクト指向のような設計は可能
|
||||||
|
|
||||||
|
|
||||||
|
## 構文例
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, World!")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* package
|
||||||
|
* 名前空間(Namespace)のようなものです。
|
||||||
|
* `package main` は、Goプログラムのエントリーポイント(実行可能なプログラム) を示します。
|
||||||
|
* `import "fmt"`
|
||||||
|
* 標準ライブラリのfmtパッケージ(文字列出力などの機能を持つ)です。
|
||||||
|
|
||||||
|
|
||||||
|
## 書き方
|
||||||
|
|
||||||
|
### 変数について
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
var message string = "Hello, Go!"
|
||||||
|
fmt.Println(message)
|
||||||
|
|
||||||
|
// 型推論
|
||||||
|
num := 42
|
||||||
|
fmt.Println(num)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `var`を使って明示的に型を指定できる
|
||||||
|
* `:=`を使うと、型推論で自動的に型が決まる(関数内のみ)
|
||||||
|
|
||||||
|
### 配列・MAP(連想配列/辞書)
|
||||||
|
|
||||||
|
#### 配列の扱い方
|
||||||
|
|
||||||
|
配列(固定長)
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 配列(固定長)
|
||||||
|
var arr [3]int = [3]int{1, 2, 3}
|
||||||
|
fmt.Println(arr)
|
||||||
|
```
|
||||||
|
|
||||||
|
配列(可変長)
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 配列(固定長)
|
||||||
|
slice := []int{1, 2, 3, 4, 5}
|
||||||
|
fmt.Println(slice)
|
||||||
|
|
||||||
|
// スライスの追加(append)
|
||||||
|
slice = append(slice, 6)
|
||||||
|
fmt.Println(slice)
|
||||||
|
|
||||||
|
// スライスの部分取り出し
|
||||||
|
fmt.Println(slice[1:4]) // [2,3,4]
|
||||||
|
```
|
||||||
|
#### MAP(連想配列/辞書)の扱い方
|
||||||
|
|
||||||
|
```go
|
||||||
|
// マップの作成
|
||||||
|
user := map[string]int{
|
||||||
|
"Alice": 25,
|
||||||
|
"Bob": 30,
|
||||||
|
}
|
||||||
|
fmt.Println(user)
|
||||||
|
|
||||||
|
// 値の取得
|
||||||
|
age := user["Alice"]
|
||||||
|
fmt.Println("Alice's age:", age)
|
||||||
|
```
|
||||||
|
|
||||||
|
キーの存在チェック
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if val, exists := user["Charlie"]; exists {
|
||||||
|
fmt.Println("Charlie is", val)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Charlie not found")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
キーの削除
|
||||||
|
|
||||||
|
```
|
||||||
|
delete(user, "Bob")
|
||||||
|
fmt.Println(user)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 構造体(struct)
|
||||||
|
|
||||||
|
* C言語のstructと似ています
|
||||||
|
* `.` を使ってフィールドにアクセス
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 構造体の定義
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// インスタンス生成
|
||||||
|
u := User{Name: "Alice", Age: 25}
|
||||||
|
fmt.Println(u)
|
||||||
|
|
||||||
|
// フィールドの変更
|
||||||
|
u.Age = 26
|
||||||
|
fmt.Println(u)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
structにメソッドを定義する
|
||||||
|
|
||||||
|
```go
|
||||||
|
// メソッドの定義(クラスのメソッドに相当)
|
||||||
|
func (u User) Greet() {
|
||||||
|
fmt.Println("Hello, my name is", u.Name)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 関数について
|
||||||
|
|
||||||
|
funcで関数を定義
|
||||||
|
|
||||||
|
```go
|
||||||
|
func greet(name string) string {
|
||||||
|
return "Hello, " + name
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 条件分岐
|
||||||
|
|
||||||
|
**if文**
|
||||||
|
|
||||||
|
* ifは( )が不要
|
||||||
|
|
||||||
|
```go
|
||||||
|
if x > 5 {
|
||||||
|
fmt.Println("x is greater than 5")
|
||||||
|
} else {
|
||||||
|
fmt.Println("x is 5 or less")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Goroutine(並行処理)
|
||||||
|
|
||||||
|
* goをつけると新しいGoroutine(軽量スレッド)として並行実行される
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func printMessage(msg string) {
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
fmt.Println(msg)
|
||||||
|
time.Sleep(time.Millisecond * 500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
go printMessage("Hello") // 並行処理で実行
|
||||||
|
go printMessage("World") // 並行処理で実行
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 3) // メインスレッドが終了しないように待つ
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Go言語のプロジェクトを実行する
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go mod init <モジュール名>
|
||||||
|
```
|
7
src/main.go
Normal file
7
src/main.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, World!")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user