forked from stevenyao/go-opencc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencc.go
More file actions
39 lines (31 loc) · 737 Bytes
/
opencc.go
File metadata and controls
39 lines (31 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package opencc
import "unsafe"
// #cgo LDFLAGS: -lopencc
// #include "opencc.h"
import "C"
type Converter struct {
id unsafe.Pointer
}
func NewConverter(config string) *Converter {
c := Converter{}
c.id = C.Opencc_New(C.CString(config));
return &c
}
func (c *Converter)Convert(input string) string {
output := C.Opencc_Convert(c.id, C.CString(input))
defer C.Opencc_Free_String(output)
return C.GoString(output)
}
func (c *Converter)Close(){
C.Opencc_Delete(c.id)
}
func Convert(input string, config string) string {
c := NewConverter(config)
defer c.Close()
return c.Convert(input)
}
func ConvertAsync(input string, config string, callback func(output string)) {
go func() {
callback(Convert(input, config))
}()
}