Sobes.tech
Junior — Middle

Is it possible to declare methods with public access in the Go language?

sobes.tech AI

Answer from AI

In the Go language, the access level to methods and fields is determined by the case of the first letter of the name:

  • If the method name starts with an uppercase letter, it is exported (public) and accessible from other packages.
  • If it starts with a lowercase letter, the method is unexported (private) and only accessible within the package.

Example:

package mypackage

type MyStruct struct {}

// Public method
func (m MyStruct) PublicMethod() {
    // ...
}

// Private method
func (m MyStruct) privateMethod() {
    // ...
}

Thus, in Go, there is no 'public' keyword, but exportability is achieved through an uppercase letter in the method name.

Is it possible to declare methods with public access… - sobes.tech