Last active
August 29, 2015 14:07
-
-
Save kyab/29938a432040d40e9f23 to your computer and use it in GitHub Desktop.
Call gnu readline library, and mruby from golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| /* | |
| #cgo CFLAGS: -I/Users/koji/work/mruby/mruby/include | |
| #cgo LDFLAGS: libmyclib.a -lreadline /Users/koji/work/mruby/mruby/build/host/lib/libmruby.a | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "myclib.h" | |
| #include <readline/readline.h> | |
| #include <readline/history.h> | |
| #include "mruby.h" | |
| #include "mruby/proc.h" | |
| */ | |
| import "C" | |
| import "unsafe" | |
| import "fmt" | |
| func Random() int { | |
| return int(C.random()) | |
| } | |
| func Seed(i int) { | |
| C.srandom(C.uint(i)) | |
| } | |
| func Sum(a int, b int) int { | |
| return int(C.sum(C.int(a),C.int(b))) | |
| } | |
| func main() { | |
| fmt.Printf("Hi callc.go\n") | |
| Seed(33); | |
| fmt.Printf("Hi ramdom = %v\n", Random()) | |
| fmt.Printf("hi sum = %v\n", Sum(2,3)) | |
| //mruby | |
| var mrb *C.mrb_state = C.mrb_open() | |
| if mrb != nil { | |
| fmt.Println("mrb_open ok") | |
| } | |
| //try using GNU readline library | |
| C.using_history() | |
| for { | |
| cinput := C.readline(C.CString("> ")) | |
| if cinput == nil { | |
| fmt.Println("nil") | |
| continue | |
| } | |
| input := C.GoString(cinput) | |
| C.add_history(cinput) | |
| C.free(unsafe.Pointer(cinput)) | |
| if input == "exit" { | |
| break | |
| } | |
| fmt.Printf(" => %v\n", input) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment