9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

go mod の使い方

Last updated at Posted at 2021-03-04

こちらの記事と同様のことを行ってみました。
GOPATH モードからモジュール対応モードへ移行せよ

go mod tidy を実行するのが異なります。

使用したバージョン

$ go version
go version go1.18.1 linux/amd64

テスト用のプログラム

hello.go
package main

import (
    "fmt"

    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Hello())
}

この時点で実行すると次のようになります。

$ go run hello.go 
hello.go:6:5: no required module provides package rsc.io/quote: working directory is not part of a module

hello モジュールの作成

go mod init hello
go mod tidy

実行時の様子

$ go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
	go mod tidy
$ go mod tidy
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2

次のファイルが作成されます。

go.mod
go.sum

フォルダーの構造

$ tree
.
├── go.mod
├── go.sum
└── hello.go

プログラムの実行

$ go run hello.go 
こんにちは世界。

次のバージョンで確認しました。

$ go version
go version go1.19.2 linux/amd64
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?