Go by conditional compilation

Source: Internet
Author: User

Go supports conditional compilation, specifically by using tags and naming conventions defined in the Go/build package to allow go packages to manage code for different platforms.

Let's take this open source project as an example to see Go's conditional compilation, an open source project that expands the Go OS package.

Https://bitbucket.org/kardianos/osext/src

Osext is the execution directory and file information for the currently executing program.

Implementation is as follows:

viewing compiled files

We use go list to see what files will be compiled in the Os/exec package under the current platform.

See what files will be compiled under the current project, with the following commands:

Here's {{. Gofiles}} is a template code in Text/template

The Go List command has the following information:
View information such as package name, path, dependencies, and so on.
Parameters:
-json: Output package information using JSON format, including the following dependencies and guides.
-f{{. Deps}}: View dependent packages, including direct or indirect dependencies.
-f{{. Imports}: View the imported packages.

D:\mycodes\golang\src\bitbucket.org\kardianos\osext>go list-f
Flag needs an argument:-F
Usage:list [-E] [-f format] [-json] [Build flags] [packages]

List lists the packages named by the import paths, one per line.

The default output shows the package import path:

Code.google.com/p/google-api-go-client/books/v1
Code.google.com/p/goauth2/oauth
Code.google.com/p/sqlite

THE-F flag specifies an alternate format for the list, using the
Syntax of the package template. The default output is equivalent to-f
‘{{. Importpath}} '. The struct being passed to the template is:

Type package struct {
Dir string//directory containing package sources
Importpath string//import path of package in Dir
Importcomment string//path in Import comment on package statement
Name string//package name
DOC string//Package documentation string
Target string//install path
goroot BOOL//Is this package in the Go root?
standard bool//are this package part of the standard Go library?

Stale bool//would ' go install ' do anything?

Root string//go root or go path dir containing this package

Source files
Gofiles []string//. Go source files (excluding cgofiles, Testgof
Iles, Xtestgofiles)
Cgofiles []string//. Go sources files that import "C"
Ignoredgofiles []string//. Go sources ignored due to build constraints
Cfiles []string//. C Source Files
Cxxfiles []string//. CC,. cxx and. cpp Source files
Mfiles []string//. M Source Files
Hfiles []string//. h,. hh,. HPP and. hXX Source files
Sfiles []string//. s source files
Swigfiles []string//. Swig files
Swigcxxfiles []string//. swigcxx files
Sysofiles []string//. Syso object files to add to archive

Cgo directives
Cgocflags []string//cgo:flags for C compiler
Cgocppflags []string//Cgo:flags for C preprocessor
Cgocxxflags []string//Cgo:flags for C + + compiler
Cgoldflags []string//CGO:FLAGS for linker
Cgopkgconfig []string//Cgo:pkg-config names

Dependency Information
Imports []string//import paths used by the This package
Deps []string//All (recursively) imported dependencies

Error Information
incomplete BOOL//This package or a dependency have an error
Error *packageerror//Error loading package
depserrors []*packageerror//Errors loading dependencies

Testgofiles []string//_test.go files in the package
Testimports []string//imports from Testgofiles
Xtestgofiles []string//_test.go files outside package
Xtestimports []string//imports from Xtestgofiles
}

The template function "join" calls strings. Join.

The template function "context" returns the build context, defined as:

Type Context struct {
Goarch string//target architecture
GOOS string//Target operating system
Goroot string//Go root
Gopath string//Go Path
cgoenabled bool//Whether CGO can be used
useallfiles bool//Use files regardless of +build lines,
File names
Compiler string//Compiler to assume when computing Targ
ET paths
Buildtags []string//Build constraints to match in +build L
Ines
Releasetags []string//Releases the current release is Compat
Ible with
Installsuffix string//suffix to use in the name of the Insta
ll dir
}

For more information on the meaning of these fields see the documentation
For the Go/build package ' s Context type.

The-json flag causes the package data to is printed in JSON format
Instead of using the template format.

THE-E flag changes the handling of erroneous packages, those that
Cannot be found or is malformed. By default, the list command
Prints an error-to-standard error-erroneous package and
Omits the packages from consideration during the usual printing.
With THE-E flag, the list command never prints errors
Error and instead processes the erroneous packages with the usual
Printing. Erroneous packages would have a non-empty importpath and
a Non-nil Error field; Other information is missing
(zeroed).

For more on build flags, see ' Go help build '.

For more on specifying packages, see ' Go to help packages '.

Compiling tags

Add labels to the source code, often called compile tags (build tag), and compile tags as close as possible to the top of the source code file and add them as annotations.
Go build reads each source file in the package while building a package and parses the compiled note, which determines whether the source file participates in this compilation

Add rules for compiling tags (attach original text):

    1. A build tag is evaluated as the OR of space-separated options
    2. Each option evaluates as the and of its comma-separated terms
    3. Each of the alphanumeric word or, preceded by!, its negation
    • Compilation tags are composed of a space-delimited compilation option (options) with a logical relationship of "or"
    • Each compilation option consists of a comma-delimited list of conditional items in a logical "and" relationship
    • The first name of each condition item is denoted by the letter + number, in front Plus! means negative.

For example, HTTPS://BITBUCKET.ORG/KARDIANOS/OSEXT/SRC here is the +build part of the Osext_procfs.go file header.

This file is compiled under the Linux NetBSD OpenBSD solaris these operating systems.

More complex compiler tags can be consulted: http://blog.csdn.net/varding/article/details/12675971

File suffix

This method provides conditional compilation by changing the suffix of the file name, which is simpler than compiling the tag, and Go/build can decide which files do not need to participate in the compilation without reading the source file.

File naming conventions can be found in the Go/build package detailed instructions, simply say if your source file contains the suffix: _$goos.go, then the source file will only be compiled under this platform, _$goarch.go. The two suffixes can be used together, but note the order: _$goos_$goarch.go, cannot be reversed with: _$goarch_$goos.go

Like, HTTPS://BITBUCKET.ORG/KARDIANOS/OSEXT/SRC, here's the file, that's the rule.

Osext_plan9.go will only be compiled in Plan9.

Osext_windows.go will only compile under Windows.

For more information, please refer to: http://blog.csdn.net/varding/article/details/12675971

Resources:

Using Go build for conditional compilation
http://blog.csdn.net/varding/article/details/12675971

Go by conditional compilation

Related Article

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.