github.com/bir3/gocompiler@v0.9.2202/README.md (about)

     1  
     2  # gocompiler
     3  
     4  The Go compiler as a package
     5  
     6  ```bash
     7  # go1.22.0
     8  go get github.com/bir3/gocompiler@v0.9.2202
     9  ```
    10  
    11  
    12  # Example
    13  
    14  
    15  ```go
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/bir3/gocompiler"
    24  )
    25  
    26  var goCodeStr string = `
    27  package main
    28  
    29  import "fmt"
    30  
    31  func main() {
    32  	fmt.Println("This code was compiled standalone")
    33  }
    34  `
    35  
    36  func main() {
    37  
    38  	// the go toolchain is built into the executable and must be given a chance to run
    39  	// => avoid side effects in init() and global variable initialization
    40  	//    as they will occur multiple times during compilation
    41  	if gocompiler.IsRunToolchainRequest() {
    42  		gocompiler.RunToolchain()
    43  		return
    44  	}
    45  
    46  	err := os.WriteFile("temp.go", []byte(goCodeStr), 0644)
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  
    51  	result, err := gocompiler.Run("go", "run", "temp.go")
    52  	fmt.Fprintf(os.Stdout, "%s", result.Stdout)
    53  	fmt.Fprintf(os.Stderr, "%s", result.Stderr)
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	os.Remove("temp.go")
    58  }
    59  ```
    60  
    61  # Standard library
    62  
    63  The standard library is embedded and is extracted on first run
    64  to standard config location;
    65  - `$HOME/.config/bir3-gocompiler` (linux)
    66  - `$HOME/Library/Application Support/bir3-gocompiler` (mac)
    67  
    68  # Private Go build cache
    69  
    70  To avoid interfering with the standard Go toolchain build cache, the package has a private 
    71  Go build cache;
    72  - `$HOME/.cache/bir3-gocompiler` (linux)
    73  - `$HOME/Library/Caches/bir3-gocompiler` (mac)
    74  
    75  # Limitations
    76  
    77  - avoid side effects in init() and global variable initializations
    78  
    79  Reason: Your executable will serve two purposes: 
    80  - run your application
    81  - run the Go compiler toolchain via `gocompiler.RunToolchain()`
    82  
    83  Side effects in init() and global variable initializations occur every time the executable is started.  
    84  The embedded Go toolchain will repeatedly start the executable during compilation to compile Go source code.  
    85  This means that global side effects like opening a http port, writing to a file or connecting to a database is likely to cause problems.
    86  
    87  
    88  # gocompiler as a package vs. the official Go toolchain
    89  
    90  |                      | "github.com/bir3/gocompiler"  | official go toolchain |                           |
    91  | -------------------  | ----------------------------- | --------------------- | ------------------------- |
    92  | Download size        | 26 MB (gzip of executable)    | 62 MB (gzip tarfile)  |                           |
    93  | Size on disk         | 91 MB                         | 237 MB                |                           |
    94  | Compile speed        | 12.9 sec                      | 12.4 sec              | macbook M1, `go build -a` |
    95  
    96  Note that this package is only focused on compiling Go source code into an executable, while the official Go toolchain provides many more tools.
    97  
    98  # Acknowledgments
    99  
   100  * The Go Authors. https://github.com/golang/go 
   101  * Klaus Post, zstd decompression: https://github.com/klauspost/compress