Source file src/cmd/cgo/internal/teststdio/testdata/stdio/file.go
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 misc/cgo/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 // Test reference to library symbol. 27 // Stdout and stderr are too special to be a reliable test. 28 //var = C.environ 29 30 func (f *File) WriteString(s string) { 31 p := C.CString(s) 32 C.fputs(p, (*C.FILE)(f)) 33 C.free(unsafe.Pointer(p)) 34 f.Flush() 35 } 36 37 func (f *File) Flush() { 38 C.fflush((*C.FILE)(f)) 39 } 40 41 var Greeting = C.GoString(C.greeting) 42 var Gbytes = C.GoBytes(unsafe.Pointer(C.greeting), C.int(len(Greeting))) 43