在go库包中包含main包的方式

// file: golang.org/x/tools/go/cfg/cfg.go
package cfg

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/format"
	"go/token"
)

// A CFG represents the control-flow graph of a single function.
//
// The entry point is Blocks[0]; there may be multiple return blocks.
type CFG struct {
	Blocks []*Block // block[0] is entry; order otherwise undefined
	noreturn bool // function body lacks a reachable return statement
}
// ...
// file: golang.org/x/tools/go/cfg/main.go

//go:build ignore

// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The cfg command prints the control-flow graph of the first function
// or method whose name matches 'funcname' in the specified package.
//
// Usage: cfg package funcname
//
// Example:
//
// $ go build -o cfg ./go/cfg/main.go
// $ cfg ./go/cfg stmt | dot -Tsvg > cfg.svg && open cfg.svg
package main

import (
"flag"
"fmt"
"go/ast"
"log"
"os"

"golang.org/x/tools/go/cfg"
"golang.org/x/tools/go/packages"
)

func main() {
	flag.Parse()
	if len(flag.Args()) != 2 {
		log.Fatal("Usage: package funcname")
		// ...
	} 
	// ...
	// ...
}

go:build ignore 标记着这个main包在以辅助库使用的场景下是ignore的,但是可以使用单独编译 main.go 来编译出main包可执行程序。

 使用Test来生成代码 TL在Go如何实现oneof语义 

Comments