エクスポートパッケージ識別子
次のコードには、一連のエクスポートされていない識別子が含まれており、すべて小文字で始まります。これらの識別子はパッケージ内で自由に使用できますが、パッケージの外部からはアクセスできません。コードは次のとおりです。
package mypkg
var myVar = 100
const myConst = "hello"
type myStruct struct {
}
これらの識別子をエクスポートするには、myStruct と myConst の最初の文字を大文字にします。変更されたコードは次のとおりです。
package mypkg
var myVar = 100
const MyConst = "hello"
type MyStruct struct {
}
現時点では、MyConst と MyStruct は外部からアクセスできます。myVar は、最初の文字が小文字であるため、mypkg パッケージ内でのみ使用でき、外部パッケージからは参照できません。
構造体とインターフェイスのメンバーをエクスポートする
エクスポートされた構造またはインターフェイスで、フィールドまたはメソッドの最初の文字が大文字の場合、これらのフィールドおよびメソッドに外部からアクセスできます。コードは次のとおりです。
type MyStruct struct {
// Fields that can be accessed from outside the package
ExportedField int
// Fields that can only be accessed from within the package
privateField int
}
type MyInterface interface {
// Methods that can be accessed from outside the package
ExportedMethod()
// Methods that can only be accessed from within the package
privateMethod()
}
コードでは、MyStruct の ExportedField および MyInterface の ExportedMethod() にパッケージの外部からアクセスできます。






![2021 年に Raspberry Pi Web サーバーをセットアップする方法 [ガイド]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)





