This is a creation in Article, where the information may have evolved or changed.
Recently in the people's post and telecommunications publishing house Xu Xiwei Lu Guihua "Go language Programming", see "1.2.9 language Interactivity" subsection of the Cprint.go. Instead of writing in the book's source code format, the import "C" and the import "unsafe" are merged into parentheses and are separated by a line with the comment Terminator * /. This is also most go language learning and developers often use code style, I modified the code as follows:
PackageMain
/*
#include <stdio.h>
#include <stdlib.h>
*/
Import(
"C"
"unsafe"
)
funcMain() {
CStr: = C.CString("Hello, World")
C.puts(CStr)
C. Free(unsafe. Pointer(CStr))
}
As a result, go build cprint.go or go run cprint.go in my Windows 7 64-bit and CentOS 6.6 x86_64 environments have been given the following error:
# command-line-arguments
Error: ' Puts ' undeclared (first use of this function)
Error: ' Free ' undeclared (first with this function)
A number of documents were looked up for 3 days, and the reason was not found, including the old version of the Go Language SDK, which was not resolved.
Occasionally see an English document, address: Http://stackoverflow.com/questions/18237738/linking-golang-with-xlib, prompted to import the virtual package C format must be a separate line, And immediately after the end of the comment line, and then re-follow the prompts to modify the compilation and run all passed, strictly according to the "Go Language Programming" book format is also normal.
The correct code is as follows:
package main
/*
#include <stdio.h>
#include <stdlib.h>
*/
import
import (
)
Span style= "color: #ffff00;" >func main () {
cstr: = C.cstring ()
c.puts (CStr)
c.free (unsafe. Pointer (CStr)
}
The Go Language code format is very strict, the majority of go language enthusiasts sometimes accidentally modified the format or the use of other language style format, compile and run there may be a variety of inexplicable problems. remind you again: import "C" must immediately follow the end of the C code comment The last line, must not empty a row, and other packages can not be merged with the import parentheses.
This article is from the "Pine" blog, be sure to keep this source http://dongsong.blog.51cto.com/916653/1587363