'Export functions from internal package in Go

I'm testing the idea of putting most of my code in an internal package and then picking exactly which of the methods / types from there I'm exposing to the outside world. Code would look like:

/mypackage/internal/catapult

package catapult

func Load(boulder Boulder) {
  // ...
}

func Trigger() *Launch {
  // ...
}

// ...

Maybe Load is being called by other internal packages (/mypackage/internal/randomevents/boredsoldier and /mypackage/internal/actualattackstrategy) but shouldn't be allowed by users outside of internal. All those are allowed to do is Trigger the catapult once it's loaded.

So now I'd like to have a package above internal (/mypackage/general) where Trigger is exposed but not Load. I was hoping to do something like:

package general

const TriggerCatapult = catapult.Trigger
// ^ does not work because a function cannot be a const

var TriggerCatapult = catapult.Trigger
// ^ technically works but now the value of TriggerCatapult can be overwritten by any package user

func TriggerCatapult() *catapult.Launch {
    return catapult.Trigger()
}
// ^ works. It's just "painful" to have to reproduce the entire function's signature every time

Is there a better way to do this?



Solution 1:[1]

No, there is no better way to do this that the way you provide:

func TriggerCatapult() *catapult.Launch {
    return catapult.Trigger()
}

You shouldn't return unexported types though, and most linters would catch this for you.

If a user is going to interact directly with things in catapult, then that package should not be internal.

Solution 2:[2]

Solved the issue, the problems was with symlinks... found the right folder and edited symlink to redirect to this folder. by default pecl installer was trying to mkdir in path

/opt/homebrew/Cellar/[email protected]/7.4.26_1/pecl

this is symlink, and it was pointing to executable file in .../bin/pecl So I edited this symlink to point to another suitable folder, this may be different for your installation. Folder called pecl, in: homebrew/lib/php/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user229044
Solution 2