github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/misc/cgo/stdio/file.go (about)

     1  // skip
     2  
     3  // 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  /*
     8  A trivial example of wrapping a C library in Go.
     9  For a more complex example and explanation,
    10  see ../gmp/gmp.go.
    11  */
    12  
    13  package stdio
    14  
    15  /*
    16  #include <stdio.h>
    17  #include <stdlib.h>
    18  #include <sys/stat.h>
    19  #include <errno.h>
    20  
    21  char* greeting = "hello, world";
    22  */
    23  import "C"
    24  import "unsafe"
    25  
    26  type File C.FILE
    27  
    28  // Test reference to library symbol.
    29  // Stdout and stderr are too special to be a reliable test.
    30  //var  = C.environ
    31  
    32  func (f *File) WriteString(s string) {
    33  	p := C.CString(s)
    34  	C.fputs(p, (*C.FILE)(f))
    35  	C.free(unsafe.Pointer(p))
    36  	f.Flush()
    37  }
    38  
    39  func (f *File) Flush() {
    40  	C.fflush((*C.FILE)(f))
    41  }
    42  
    43  var Greeting = C.GoString(C.greeting)
    44  var Gbytes = C.GoBytes(unsafe.Pointer(C.greeting), C.int(len(Greeting)))