Golang conditional compilation

后端 未结 4 480
南方客
南方客 2020-12-23 13:04

I\'ve got a trouble with conditional compilation in Go 1.

Here is my test code. Is there anything I misunderstand about the \"// +build\" constraint and the \"-tags\

相关标签:
4条回答
  • 2020-12-23 13:07

    http://golang.org/pkg/go/build/ Build Constraints “To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.”

    0 讨论(0)
  • 2020-12-23 13:18

    You must follow // +build XXX with a blank line.

    In my brief search, I couldn't find where/if this is documented. But the source clearly calls it out

    0 讨论(0)
  • 2020-12-23 13:21

    Package build

    Build Constraints

    A build constraint is a line comment beginning with the directive +build that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments.

    To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

    Add a blank line after the build constraint. For example,

    // +build main1
    
    package main
    
    import (
        "fmt"
    )
    
    func main() {
        fmt.Println("This is main 1")
    }
    
    0 讨论(0)
  • 2020-12-23 13:27

    Right, you must leave a blank line, not exactly after // +build XXX but before package main because all the comment lines before the line declaring the package are considered to be the description of the package and parsed by godoc.

    0 讨论(0)
提交回复
热议问题