-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
32 lines (26 loc) · 720 Bytes
/
main.go
File metadata and controls
32 lines (26 loc) · 720 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
package main
// #include <stdlib.h>
import "C"
import (
"fmt"
)
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
//export reverseString
func reverseString(input *C.char) *C.char {
// converts passed string pointer to go string
stringToReverse := C.GoString(input)
fmt.Printf("Go says: reversing string %s\n", stringToReverse)
// converts Go string to a C string
reversedCString := C.CString(reverse(stringToReverse))
return reversedCString
}
func main() {
// to test with go run main.go
fmt.Printf("Reversing string in Go: %s\n", reverse("Hello!"))
}