1
2

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

Golangで外部パッケージから呼び出す際に躓きがちなこと

Last updated at Posted at 2019-09-18

Golangの初学者が、ハマりがちな外部パッケージでの構造体(関数等々)呼び出しに関して書いておきます!

importするだけじゃ不十分

下記のコードでmodelに書いた構造体(user)を外部パッケージでは呼び出せません。

model/user.go
package model

type user struct {
	name string
	age  int
}
controller/user.go
package controller

import (
	"${PATH}/model"
)

var user model.user //undefined: model.user


//undefined: model.user
ってなります。

解決策

model/user.go
package model

type User struct {
	name string
	age  int
}
controller/user.go
package controller

import (
	"${PATH}/model"
)

var user model.User


参考:https://qiita.com/zurazurataicho/items/4a95e0daf0d960cfc2f7

参照したい構造体(や関数)を大文字にするだけで読み込める!

多分躓くと思うので書いておきます!

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?