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 3 years have passed since last update.

パッケージの分け・呼び出し

Posted at

パッケージを作る

ディレクトリを作成してgoファイルを作成
ファイル名はパッケージの役割がわかるように一致させるのが好ましい

image.png

greeting/hello.go
//パッケージはディレクトリ名
package greeting

//呼び出しができるように大文字から始める
func Do() string {
	hello := "Hello for greeting"
	return hello
}

greeting/sub/sub.go
package sub

func Sub() {
	println("sub func")
}

呼び出し方法

main.go
package main

import (
//importはプロジェクト名からパッケージがあるディレクトリ名まで
	"udemy/greeting"
	"udemy/greeting/sub"
)

func main() {
//呼び出しはディレクトリ名.関数
	hello := greeting.Do()
	fmt.Println(hello)
	sub.Sub()
}
0
0
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
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?