This is a creation in Article, where the information may have evolved or changed.
Golang Rich I/O two ----version cgo Hello World
InGolangRich inI/o----UseNTwoHello WorldShow "in a variety ofHello WorldThe wording of this showGolangRich and powerfulI/Ofunction, in addition to aCGOVersion ofHello World。 The following code is derived fromGoSource:
Main.go
Package Mainimport "Stdio" Func Main () { stdio. Stdout.writestring (stdio. Greeting + "\ n")}
File.go
skip//Copyright Authors the Go. All rights reserved.//Use of this source code are governed by a bsd-style//license so can be found in the license file. /*a Trivial example of wrapping a C library in go.for A more complex example and Explanation,see. /gmp/gmp.go.*/package stdio/* #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <errno.h>char* greeting = "Hello, world"; */import "C" import "unsafe" Typefile c.file//Test reference to Library symbol.//Stdout and stderr is too special to be a reliable Test.//var = C.environfunc (f *file) WriteString (s string) { C0/>p: = c.cstring (s) c.fputs (P, (*c.file) (f)) c.free (unsafe. Pointer (p)) F.flush ()}func (f *file) Flush () { C.fflush ((*c.file) (f))}var greeting = c.gostring (c.greeting) var gbytes = c.gobytes (unsafe. Pointer (c.greeting), C.int (len (greeting)))
Stdio.go
skip//Copyright Authors the Go. All rights reserved.//Use of this source code are governed by a bsd-style//license so can be found in the license file. Package stdio/* #include <stdio.h>//on MinGW, stderr and stdout is defined as &_iob[fileno]//on NetBSD, they a Re defined as &__sf[fileno]//and CGO doesn ' t recognize them, so write a function to get them,//instead of depending On internals of LIBC implementation. FILE *getstdout (void) {return stdout;} FILE *getstderr (void) {return stderr;} */import "C" var Stdout = (*file) (C.getstdout ()) var Stderr = (*file) (C.getstderr ())
GoProgram can be passedCGOTool makes it very easy to invokeCFunction. AboutGoCallC + +OrC + +CallGoThe program can refer to the previous series of essays "C + +CallGolangandcalling C + + from Golang with swig---windows DLLs》