GithubHelp home page GithubHelp logo

go-turtorial's Introduction

Go Turtorial

参考資料

プログラミング言語Go完全入門

各種チュートリアル

開発環境構築

DockerCompose構築

Dockerfile作成

# goバージョン
FROM golang:1.20.1-alpine
# アップデートとgitのインストール
RUN apk update && apk add git
# boiler-plateディレクトリの作成
RUN mkdir /go/src/app
# ワーキングディレクトリの設定
WORKDIR /go/src/app
# ホストのファイルをコンテナの作業ディレクトリに移行
ADD . /go/src/app

docker-compose.yml作成

version: '3' # composeファイルのバージョン
services:
  turtorial: # サービス名
    container_name: turtorial
    build: # ビルドに使うDockerファイルのパス
      context: .
      dockerfile: ./build/Dockerfile
    volumes: # マウントディレクトリ
      - ./cmd:/go/src/app
    tty: true # コンテナの永続化
    environment:
      - TZ=Asia/Tokyo

以下のコマンドを実行

docker-compose up -d —build

cmdディレクトリを作成し、以下にmain.goファイルを作成する

package main

import "fmt"

func main() {
	fmt.Println("Hello world")
}

以下のコマンドを実行

$ docker exec -it turtorial sh
/go/src/app # go run main.go
Hello world

VisualStudioCode構築

VisualStudioCodeの拡張機能で提供されているGoをインストールする

モジュール管理

Goにおけるモジュール管理はgo mod tidyを使用する。

Qiita - go mod tidyの役割 Qiita - go mod tidy で不要なpackageを削除する

/go/src/app # go mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod <command> [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

初期化

初期化としてgo.modを作成する

/go/src/app # go mod init forests-k/turtorial
go: creating new go.mod: module forests-k/turtorial
go: to add module requirements and sums:
        go mod tidy

パッケージをインストールすると、go.sumファイルが作成される

/go/src/app # go get golang.org/x/tools/cmd/goimports
go: downloading golang.org/x/tools v0.6.0
go: downloading golang.org/x/sys v0.5.0
go: downloading golang.org/x/mod v0.8.0
go: added golang.org/x/mod v0.8.0
go: added golang.org/x/sys v0.5.0

標準的なライブラリとしては、以下がある

  • fmt
    • 書式に関する処理
  • net/http
    • HTTPサーバ系
  • archive,compress
    • zipやgzipなど
  • encoding
    • JSON、XML、CSVなど
  • html/template
    • HTMLテンプレート
  • os,path/filepath
    • ファイル操作

Zenn - dockerでgo開発環境構築

各ツール

  • go build
    • ビルドコマンド
  • go test
    • テストコード実行
  • go doc
    • ドキュメント生成
  • gofmt
    • ソールコードフォーマッタ
  • go vet
    • コードチェッカ
  • gopls
    • Language Server Protocol実装

go-turtorial's People

Contributors

forests-k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.