LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2009 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 /* |
| 6 A trivial example of wrapping a C library in Go. |
| 7 For a more complex example and explanation, |
| 8 see ../gmp/gmp.go. |
| 9 */ |
| 10 |
| 11 package stdio |
| 12 |
| 13 /* |
| 14 #include <stdio.h> |
| 15 #include <stdlib.h> |
| 16 #include <sys/stat.h> |
| 17 #include <errno.h> |
| 18 |
| 19 char* greeting = "hello, world"; |
| 20 */ |
| 21 import "C" |
| 22 import "unsafe" |
| 23 |
| 24 type File C.FILE |
| 25 |
| 26 var Stdout = (*File)(C.stdout) |
| 27 var Stderr = (*File)(C.stderr) |
| 28 |
| 29 // Test reference to library symbol. |
| 30 // Stdout and stderr are too special to be a reliable test. |
| 31 var myerr = C.sys_errlist |
| 32 |
| 33 func (f *File) WriteString(s string) { |
| 34 p := C.CString(s) |
| 35 C.fputs(p, (*C.FILE)(f)) |
| 36 C.free(unsafe.Pointer(p)) |
| 37 f.Flush() |
| 38 } |
| 39 |
| 40 func (f *File) Flush() { |
| 41 C.fflush((*C.FILE)(f)) |
| 42 } |
| 43 |
| 44 var Greeting = C.GoString(C.greeting) |
LEFT | RIGHT |