I want to access a private function of a package called "pastry". But it generates error as: invalid reference to unexported identifier
Specify the way by which private functions of golang are accessible in main.
I want to access a private function of a package called "pastry". But it generates error as: invalid reference to unexported identifier
Specify the way by which private functions of golang are accessible in main.
You can use go:linkname to map functions from same/different package
to some function in yours. For example like:
package main
import (
"fmt"
_ "net"
_ "unsafe"
)
//go:linkname lookupStaticHost net.lookupStaticHost
func lookupStaticHost(host string) []string
func main() {
fmt.Println(lookupStaticHost("localhost"))
}
will produce [127.0.0.1 ::1] when executed on my machine.
Private functions are, by definition, not accessible outside the package in which they are declared.
If you need that function outside that package, you must make it public (change the function name, turning the first letter in upper case).
Eg: if you have func doSomething() rename it to func DoSomething() and use it outside the package with <package name>.DoDomething()
in the package (let's say mypackage) where you have pastry function add:
var Pastry = pastry
in main package:
mypackage.Pastry()