Skip to content

Commit

Permalink
优化main包,可以单独以命令行的方式运行程序
Browse files Browse the repository at this point in the history
  • Loading branch information
vito-go committed Mar 8, 2024
1 parent 367ed05 commit d38981a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
32 changes: 32 additions & 0 deletions mywords-go/cmd/flutter/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import "C"
import (
"mywords/dict"
"mywords/server"
"sync"
"time"
)

func init() {
// flutter 中的日志是 UTC 时间,所以这里要设置时区
time.Local = time.FixedZone("CST", 8*3600)
}

var once sync.Once

//export Init
func Init(rootDataDir *C.char, proxyUrl *C.char) {
once.Do(func() {
initGlobal(C.GoString(rootDataDir), C.GoString(proxyUrl))
})
}
func initGlobal(rootDataDir string, proxyUrl string) {
srv, err := server.NewServer(rootDataDir, proxyUrl)
if err != nil {
panic(err)
}
serverGlobal = srv
multiDictGlobal = dict.NewMultiDictZip(rootDataDir)
go multiDictGlobal.Init()
}
40 changes: 12 additions & 28 deletions mywords-go/cmd/flutter/main.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
//go:build !flutter

package main

import "C"
import (
"fmt"
"mywords/dict"
"mywords/server"
"sync"
"time"
"os"
"path/filepath"
)

// 可以命令行的方式运行
func main() {
// must Init when using the exported method in this package
fmt.Println("Hello World from Flutter")
}

func init() {
// flutter 中的日志是 UTC 时间,所以这里要设置时区
time.Local = time.FixedZone("CST", 8*3600)
}

var once sync.Once

//export Init
func Init(rootDataDir *C.char, proxyUrl *C.char) {
once.Do(func() {
initGlobal(C.GoString(rootDataDir), C.GoString(proxyUrl))
})
}
func initGlobal(rootDataDir string, proxyUrl string) {
srv, err := server.NewServer(rootDataDir, proxyUrl)
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
initGlobal(filepath.Join(homeDir, ".local/share/com.example.mywords"), "")
err = serverGlobal.ShareOpen(18964, 890604)
if err != nil {
panic(err)
}
serverGlobal = srv
multiDictGlobal = dict.NewMultiDictZip(rootDataDir)
go multiDictGlobal.Init()
select {}
}
13 changes: 13 additions & 0 deletions mywords-go/cmd/flutter/main_flutter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build flutter

package main

import (
"fmt"
)

func main() {
// 编译为so库供flutter使用
// must Init when using the exported method in this package
fmt.Println("Hello World from Flutter")
}
6 changes: 3 additions & 3 deletions mywords-go/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ build-so: build-so-linux build-so-android
@echo "build .so for all platform successfully"
build-so-linux:
@echo "build so for linux platform"
@go build -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/libs/libgo_linux.so ./cmd/flutter/
@go build -tags=flutter -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/libs/libgo_linux.so ./cmd/flutter/
@echo "build .so for linux platform successfully!"
build-so-windows:
@echo "build so for windows platform"
@go build -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/libs/libgo_windows.so ./cmd/flutter/
@go build -tags=flutter -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/libs/libgo_windows.so ./cmd/flutter/
@echo "build .so for windows platform successfully!"
build-so-android:
@echo "build .so for android platform"
@CC=${CC} GOOS=android CGO_ENABLED=1 GOARCH=arm64 go build -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/android/app/jniLibs/arm64-v8a/libgo.so ./cmd/flutter/
@CC=${CC} GOOS=android CGO_ENABLED=1 GOARCH=arm64 go build -tags=flutter -buildmode=c-shared -ldflags="-s -w" -o $(FLUTTER_DIR)/android/app/jniLibs/arm64-v8a/libgo.so ./cmd/flutter/
@echo "build .so for android platform successfully!"

0 comments on commit d38981a

Please sign in to comment.