0
0

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言語でwindowローカル環境の別のフォルダーのファイルの関数を呼び出す方法

Posted at

go言語で別フォルダーの関数を呼ぶ時、依存関係でハマったので纏めます
今回はtestフォルダーのtest.goを呼ぶ簡単な構成で説明します

ファイル構成

gotest
∟test
 ∟test.go
∟main.go

main.goのsrcの中身

package main

import (
	"fmt"

	"gotest/test"
)

func main() {
	fmt.Println("test")
	test.GetTest()
}

test.goのsrcの中身

package test

import (
	"fmt"
)

func GetTest() {
	fmt.Println("test2")
}

modの初期化やpackageの取得

// modの初期化
$ go mod init gotest

// フォルダー移動
$ cd test

// modの初期化
$ go mod init gotest/test

// 上階層に移動
$ cd ..

// testフォルダーのpackageをローカルの相対パスに変更
$ go mod edit -replace=gotest/test=./test

// packageを取得
$ go get gotest/test

// main.goを実行
$ go run main.go

実行され下記のように出力される

test
test2

go言語を勉強しはじめた時にハマりやすいポイントなので参考にして頂ければと思います

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?