feat(loader): make Load idempotent and expose TA-Lib version API
- guard library initialization with sync.Once to avoid duplicate registrations - refactor loading logic into internalLoad() - register TA_GetVersionString and expose Version() - document Version() usage and API behavior in README
This commit is contained in:
@@ -1,16 +1,40 @@
|
||||
package talib
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/ebitengine/purego"
|
||||
)
|
||||
|
||||
var (
|
||||
internalVersion func() string
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func Version() string {
|
||||
return internalVersion()
|
||||
}
|
||||
|
||||
func Load() (uintptr, error) {
|
||||
var (
|
||||
ptr uintptr
|
||||
err error
|
||||
)
|
||||
|
||||
once.Do(func() {
|
||||
ptr, err = internalLoad()
|
||||
})
|
||||
|
||||
return ptr, err
|
||||
}
|
||||
|
||||
func internalLoad() (uintptr, error) {
|
||||
var (
|
||||
path string
|
||||
ptr uintptr
|
||||
@@ -26,6 +50,8 @@ func Load() (uintptr, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
purego.RegisterLibFunc(&internalVersion, ptr, "TA_GetVersionString")
|
||||
|
||||
// Register the functions we need from the TA-Lib library
|
||||
purego.RegisterLibFunc(&ht_dcperiod, ptr, "TA_HT_DCPERIOD")
|
||||
purego.RegisterLibFunc(&ht_dcphase, ptr, "TA_HT_DCPHASE")
|
||||
|
||||
Reference in New Issue
Block a user