Go wrapper for OpenCC (Open Chinese Convert) using WebAssembly, enabling Chinese text conversion between Simplified and Traditional Chinese.
- Convert between Simplified and Traditional Chinese
- Support for various regional variants (Taiwan, Hong Kong)
- WebAssembly-based implementation for cross-platform compatibility
- High performance with wazero runtime
- Thread-safe operations
- Memory efficient
go get github.com/bestnite/go-opencc
To build the WebAssembly module, you need:
- WASI SDK: Download from wasi-sdk releases
- CMake: Version 3.5 or higher
- wasm-opt (optional): For optimization, install from binaryen
- Clone the repository with submodules:
git clone --recursive https://github.com/bestnite/go-opencc.git
cd go-opencc
- Build the WebAssembly module:
./build.sh
- Download Go dependencies:
go mod tidy
package main
import (
"fmt"
"log"
"github.com/bestnite/go-opencc"
)
func main() {
// Convert Simplified to Traditional Chinese
result, err := opencc.ConvertS2T("简体字")
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Output: 簡體字
// Convert Traditional to Simplified Chinese
result, err = opencc.ConvertT2S("繁體字")
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Output: 繁体字
}
For better performance when doing multiple conversions, use a converter instance:
package main
import (
"fmt"
"log"
"github.com/bestnite/go-opencc"
)
func main() {
// Create a converter instance
converter, err := opencc.NewConverter("s2t.json")
if err != nil {
log.Fatal(err)
}
defer converter.Close()
// Convert multiple texts
texts := []string{"简体字", "测试", "转换"}
for _, text := range texts {
result, err := converter.Convert(text)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s -> %s\n", text, result)
}
}
Converts Simplified Chinese to Traditional Chinese.
Converts Traditional Chinese to Simplified Chinese.
Creates a new converter instance with the specified configuration file.
Represents an OpenCC converter instance.
Methods:
Convert(input string) (string, error)
- Converts text using the converterClose() error
- Closes the converter and releases resources
ErrInvalidConverter
- Returned when converter creation failsErrConversionFailed
- Returned when text conversion fails
Run tests:
go test
Run benchmarks:
go test -bench=.
This implementation uses WebAssembly with the wazero runtime, providing:
- Fast startup times
- Low memory overhead
- Thread-safe operations
- Cross-platform compatibility
This project is licensed under the Apache License 2.0 - see the OpenCC project for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run tests and ensure they pass
- Submit a pull request