Golang Rich I/O two----version cgo Hello World

Source: Internet
Author: User
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》

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.