blob: e7bb906a54b354f0e8264f16a08267dbcb854a2c [file] [log] [blame]
Shenghou Ma1e954292012-08-07 09:38:35 +08001// skip
2
Russ Cox133a1582009-10-03 10:37:12 -07003// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7/*
8A trivial example of wrapping a C library in Go.
9For a more complex example and explanation,
10see ../gmp/gmp.go.
11*/
12
13package stdio
14
Russ Cox133a1582009-10-03 10:37:12 -070015/*
16#include <stdio.h>
17#include <stdlib.h>
Russ Cox0432f282010-07-14 17:17:53 -070018#include <sys/stat.h>
19#include <errno.h>
Russ Cox133a1582009-10-03 10:37:12 -070020
Russ Cox0432f282010-07-14 17:17:53 -070021char* greeting = "hello, world";
Russ Cox133a1582009-10-03 10:37:12 -070022*/
23import "C"
24import "unsafe"
25
Russ Cox133a1582009-10-03 10:37:12 -070026type File C.FILE
27
Russ Cox6c6d5302010-12-17 11:37:11 -080028// Test reference to library symbol.
29// Stdout and stderr are too special to be a reliable test.
Russ Coxc3f43192012-03-06 23:27:30 -050030//var = C.environ
Russ Cox6c6d5302010-12-17 11:37:11 -080031
Russ Cox133a1582009-10-03 10:37:12 -070032func (f *File) WriteString(s string) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080033 p := C.CString(s)
Russ Cox0432f282010-07-14 17:17:53 -070034 C.fputs(p, (*C.FILE)(f))
Robert Griesemer5a1d3322009-12-15 15:33:31 -080035 C.free(unsafe.Pointer(p))
Russ Cox0432f282010-07-14 17:17:53 -070036 f.Flush()
Russ Cox133a1582009-10-03 10:37:12 -070037}
Russ Cox0432f282010-07-14 17:17:53 -070038
39func (f *File) Flush() {
40 C.fflush((*C.FILE)(f))
41}
42
43var Greeting = C.GoString(C.greeting)
Russ Coxdb9229d2011-07-28 12:39:50 -040044var Gbytes = C.GoBytes(unsafe.Pointer(C.greeting), C.int(len(Greeting)))