Sobes.tech
Junior — Middle

Kas on võimalik deklareerida meetodeid avaliku juurdepääsuga Go keeles?

sobes.tech AI

Vastus AI-lt

In the Go language, the access level to methods and fields is determined by 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 keyword public, but exportability is achieved through an uppercase letter at the beginning of the method name.