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/* 6A trivial example of wrapping a C library in Go. 7For a more complex example and explanation, 8see misc/cgo/gmp/gmp.go. 9*/ 10 11package stdio 12 13/* 14#include <stdio.h> 15#include <stdlib.h> 16#include <sys/stat.h> 17#include <errno.h> 18 19char* greeting = "hello, world"; 20*/ 21import "C" 22import "unsafe" 23 24type 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 30func (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 37func (f *File) Flush() { 38 C.fflush((*C.FILE)(f)) 39} 40 41var Greeting = C.GoString(C.greeting) 42var Gbytes = C.GoBytes(unsafe.Pointer(C.greeting), C.int(len(Greeting))) 43