In Go programming language, functions can be defined as either private or public, which determines their accessibility from outside the package. A function can be accessed from another package only if it is declared as public, while a private function can only be accessed within the same package.
In Go, the naming convention for public functions is to start the function name with a capital letter. On the other hand, functions that start with a lowercase letter are considered private.
Here's an example of a public function in Go:
package main
import "fmt"
func Add(a, b int) int {
return a + b
}
func main() {
sum := Add(5, 6)
fmt.Println(sum) // Output: 11
diff := subtract(10, 5) // Error: Cannot access private function
fmt.Println(diff)
}
In this example, the function Add is declared as public because it starts with a capital letter. The function takes two integer arguments a and b, and returns their sum. The function can be accessed from outside the package, as demonstrated in the main function.
Here's an example of a private function in Go:
package calculator
// Public function to add two numbers
func Add(a, b int) int {
return a + b
}
// Private function to subtract two numbers
func subtract(a, b int) int {
return a - b
}
In this example, the Add function is declared as public because it starts with a capital letter, while the subtract function is declared as private because it starts with a lowercase letter. Both functions are defined within the same package called calculator.
Now, let's say we want to use the Add function from the calculator package in our main package:
package main
import (
"fmt"
"calculator"
)
func main() {
sum := calculator.Add(5, 6)
fmt.Println(sum) // Output: 11
diff := calculator.subtract(10, 5) // Error: Cannot access private function
fmt.Println(diff)
}
In this example, we are able to use the public Add function from the calculator package in our main package, and we get the expected output of 11.
However, when we try to use the private subtract function from the calculator package in our main package, we get a compile-time error that says "calculator.subtract undefined (cannot refer to unexported name calculator.subtract)".
This error occurs because we are trying to access a private function from another package, which is not allowed in Go.
To fix this error, we can either change the subtract function to a public function by starting its name with a capital letter, or we can move the main package into the same package as the calculator package so that it can access the private function.
In summary, understanding the difference between private and public functions is important when working with Go packages. By using the naming convention of starting public function names with capital letters and private function names with lowercase letters, we can control the accessibility of functions within our packages. Trying to access a private function from outside the package will result in a compile-time error, so it's important to make sure that functions that need to be accessed from outside the package are declared as public.
Comments