github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/test/fixedbugs/bug369.go (about)

     1  // +build !nacl,!windows
     2  // run
     3  
     4  // Copyright 2011 The Go Authors.  All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // Test that compiling with optimization turned on produces faster code.
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"go/build"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  )
    19  
    20  func main() {
    21  	a, err := build.ArchChar(build.Default.GOARCH)
    22  	check(err)
    23  
    24  	err = os.Chdir(filepath.Join(".", "fixedbugs", "bug369.dir"))
    25  	check(err)
    26  
    27  	run("go", "tool", a+"g", "-N", "-o", "slow."+a, "pkg.go")
    28  	run("go", "tool", a+"g", "-o", "fast."+a, "pkg.go")
    29  	run("go", "tool", a+"g", "-o", "main."+a, "main.go")
    30  	run("go", "tool", a+"l", "-o", "a.exe", "main."+a)
    31  	run("." + string(filepath.Separator) + "a.exe")
    32  
    33  	os.Remove("slow." + a)
    34  	os.Remove("fast." + a)
    35  	os.Remove("main." + a)
    36  	os.Remove("a.exe")
    37  }
    38  
    39  func run(name string, args ...string) {
    40  	cmd := exec.Command(name, args...)
    41  	out, err := cmd.CombinedOutput()
    42  	if err != nil {
    43  		fmt.Println(string(out))
    44  		fmt.Println(err)
    45  		os.Exit(1)
    46  	}
    47  }
    48  
    49  func check(err error) {
    50  	if err != nil {
    51  		fmt.Println(err)
    52  		os.Exit(1)
    53  	}
    54  }