Skip to content

Commit

Permalink
add golang bind example
Browse files Browse the repository at this point in the history
  • Loading branch information
ququzone committed Jan 16, 2023
1 parent d062ecb commit 820eb57
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ctss bind examples
==================

### Build golang

```
go build -ldflags="-r lib" keygen.go
```
2 changes: 2 additions & 0 deletions examples/golang/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/keygen
/local-share*.json
39 changes: 39 additions & 0 deletions examples/golang/keygen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

/*
#cgo LDFLAGS: -L./lib -lctss
#include "./lib/ctss.h"
*/
import "C"
import (
"fmt"
"log"
"os"
"strconv"
)

func main() {
fmt.Println("input index:")
var indexStr string
_, err := fmt.Scanln(&indexStr)
if err != nil {
log.Fatalln(err)
}

index, err := strconv.Atoi(indexStr)
if err != nil {
log.Fatalln(err)
}
data := C.keygen(C.CString("http://localhost:8000/"), C.CString("default-keygen"), C.uint16_t(index), 1, 3)

file, err := os.Create(fmt.Sprintf("local-share%d.json", index))
if err != nil {
log.Fatalln(err)
}
defer file.Close()

_, err = file.WriteString(C.GoString(data))
if err != nil {
log.Fatalln(err)
}
}
7 changes: 7 additions & 0 deletions examples/golang/lib/ctss.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdint.h>

const char *keygen(const char *server_url,
const char *room,
uint16_t index,
uint16_t threshold,
uint16_t number_of_parties);
Binary file added examples/golang/lib/libctss.dylib
Binary file not shown.

0 comments on commit 820eb57

Please sign in to comment.