Golang类似于C的静态语言,效率也接近于C,如果Golang也可以导出可供C调用的库,那可以和很多高级语言say goodbye了,goodbye似乎又有点武断,但至少说,Golang可以做很多事,而且效率优于很多高级语言,这样说应该没有问题。
接下来,就从三个方面分别来介绍Golang中关于库的使用。
##Using Share Library
The latest Go 1.5 version is out. As part of the new features, Go compiler can compile packages as a shared libraries.
It accepts -buildmode argument that determines how a package is compiled. These are the following options:
* archive: Build the listed non-main packages into .a files. Packages named main are ignored.
* c-archive: Build the listed main package, plus all packages it imports, into a C archive file.
* c-shared: Build the listed main packages, plus all packages that they import, into C shared libraries.
* shared: Combine all the listed non-main packages into a single shared library.
* exe: Build the listed main packages and everything they import into executables. Packages not named main are ignored.
By default, listed main packages are built into executables and listed non-main packages are built into .a files.
平时编译、运行golang,可能用得比较多的是下面这些:
go run xxx.go //直接运行xxx.go,有点类似解释语言,比如lua, python,其实执行go run,是对源文件做了build,然后再run执行文件的,只是这些都在后台做了
go build xxx.go //编译产生同名的执行档,如果在源文件目录下直接go build会产生与目录名同名的可执行档
go install xxx.go //也是编译,只是编译后会将同名的执行档安装到$GOPATH/bin下面,同样在源文件目录下直接go install,会把与目录同名的可执行档安装在$GOPATH/bin下
以上的go build都是默认的编译,也就是-buildmode是default的
By default, listed main packages are built into executables and listed non-main packages are built into .a files.
现在进入正题,这里我主要是要说明下面这个选项的使用方法:
* shared: Combine all the listed non-main packages into a single s