本文整理汇总了Golang中runtime.GOROOT函数的典型用法代码示例。如果您正苦于以下问题:Golang GOROOT函数的具体用法?Golang GOROOT怎么用?Golang GOROOT使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GOROOT函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FillSettingsDefaults
//TODO fulfil all defaults
func FillSettingsDefaults(settings *Settings, workingDirectory string) {
if settings.AppName == "" {
settings.AppName = core.GetAppName(settings.AppName, workingDirectory)
}
if settings.OutPath == "" {
settings.OutPath = core.OUTFILE_TEMPLATE_DEFAULT
}
if settings.ResourcesInclude == "" {
settings.ResourcesInclude = core.RESOURCES_INCLUDE_DEFAULT
}
if settings.ResourcesExclude == "" {
settings.ResourcesExclude = core.RESOURCES_EXCLUDE_DEFAULT
}
if settings.MainDirsExclude == "" {
settings.MainDirsExclude = core.MAIN_DIRS_EXCLUDE_DEFAULT
}
if settings.PackageVersion == "" {
settings.PackageVersion = core.PACKAGE_VERSION_DEFAULT
}
if settings.BuildSettings == nil {
bs := BuildSettings{}
FillBuildSettingsDefaults(&bs)
settings.BuildSettings = &bs
}
if settings.GoRoot == "" {
if settings.IsVerbose() {
log.Printf("Defaulting GoRoot to runtime.GOROOT (%s)", runtime.GOROOT())
}
settings.GoRoot = runtime.GOROOT()
}
}
示例2: TestGZIPFilesHaveZeroMTimes
// Per golang.org/issue/14937, check that every .gz file
// in the tree has a zero mtime.
func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
if testing.Short() && testenv.Builder() == "" {
t.Skip("skipping in short mode")
}
var files []string
err := filepath.Walk(runtime.GOROOT(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".gz") {
files = append(files, path)
}
return nil
})
if err != nil {
if os.IsNotExist(err) {
t.Skipf("skipping: GOROOT directory not found: %s", runtime.GOROOT())
}
t.Fatal("error collecting list of .gz files in GOROOT: ", err)
}
if len(files) == 0 {
t.Fatal("expected to find some .gz files under GOROOT")
}
for _, path := range files {
checkZeroMTime(t, path)
}
}
示例3: TestFixedGOROOT
func TestFixedGOROOT(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skipf("skipping plan9, it is inconsistent by allowing GOROOT to be updated by Setenv")
}
// Restore both the real GOROOT environment variable, and runtime's copies:
if orig, ok := syscall.Getenv("GOROOT"); ok {
defer syscall.Setenv("GOROOT", orig)
} else {
defer syscall.Unsetenv("GOROOT")
}
envs := runtime.Envs()
oldenvs := append([]string{}, envs...)
defer runtime.SetEnvs(oldenvs)
// attempt to reuse existing envs backing array.
want := runtime.GOROOT()
runtime.SetEnvs(append(envs[:0], "GOROOT="+want))
if got := runtime.GOROOT(); got != want {
t.Errorf(`initial runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Setenv("GOROOT", "/os"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after setenv runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Unsetenv("GOROOT"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after unsetenv runtime.GOROOT()=%q, want %q`, got, want)
}
}
示例4: subdir
// subdir determines the package based on the current working directory,
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
func subdir() (pkgpath string, underGoRoot bool, err error) {
cwd, err := os.Getwd()
if err != nil {
return "", false, err
}
if root := runtime.GOROOT(); strings.HasPrefix(cwd, root) {
subdir, err := filepath.Rel(root, cwd)
if err != nil {
return "", false, err
}
return subdir, true, nil
}
for _, p := range filepath.SplitList(build.Default.GOPATH) {
if !strings.HasPrefix(cwd, p) {
continue
}
subdir, err := filepath.Rel(p, cwd)
if err == nil {
return subdir, false, nil
}
}
return "", false, fmt.Errorf(
"working directory %q is not in either GOROOT(%q) or GOPATH(%q)",
cwd,
runtime.GOROOT(),
build.Default.GOPATH,
)
}
示例5: Asm
func (t *gcToolchain) Asm(pkg *Package, srcdir, ofile, sfile string) error {
args := []string{"-o", ofile, "-D", "GOOS_" + pkg.gotargetos, "-D", "GOARCH_" + pkg.gotargetarch}
switch {
case goversion == 1.4:
includedir := filepath.Join(runtime.GOROOT(), "pkg", pkg.gotargetos+"_"+pkg.gotargetarch)
args = append(args, "-I", includedir)
case goversion > 1.4:
odir := filepath.Join(filepath.Dir(ofile))
includedir := filepath.Join(runtime.GOROOT(), "pkg", "include")
args = append(args, "-I", odir, "-I", includedir)
default:
return fmt.Errorf("unsupported Go version: %v", runtime.Version)
}
args = append(args, sfile)
if err := mkdir(filepath.Dir(ofile)); err != nil {
return fmt.Errorf("gc:asm: %v", err)
}
var buf bytes.Buffer
err := runOut(&buf, srcdir, nil, t.as, args...)
if err != nil {
fmt.Fprintf(os.Stderr, "# %s\n", pkg.ImportPath)
io.Copy(os.Stderr, &buf)
}
return err
}
示例6: StdPkg
// StdPkg returns all lists of Go standard packages.
// Usually pass "/usr/local/go".
// There is an alternative way: https://github.com/golang/tools/blob/master/imports/mkstdlib.go
func StdPkg(goRootPath string) (map[string]bool, error) {
if goRootPath == "" {
goRootPath = runtime.GOROOT()
if goRootPath == "" {
goRootPath = os.Getenv("GOROOT")
if goRootPath == "" {
return nil, errors.New("can't find GOROOT: try to set it to /usr/local/go")
}
}
}
stdpkgPath := filepath.Join(goRootPath, "src")
rmap, err := walkDir(stdpkgPath)
if err != nil {
log.Println("trying to find the runtime `GOROOT`")
goRootPath = runtime.GOROOT()
stdpkgPath = filepath.Join(goRootPath, "src")
log.Println("try with:", stdpkgPath)
rmap, err = walkDir(stdpkgPath)
if err != nil {
log.Println("trying to find the environment variable `GOROOT`")
goRootPath = os.Getenv("GOROOT")
stdpkgPath = filepath.Join(goRootPath, "src")
log.Println("try with:", stdpkgPath)
rmap, err = walkDir(stdpkgPath)
if err != nil {
return nil, err
}
}
}
smap := make(map[string]bool)
for _, val := range rmap {
stdName := strings.Replace(val, stdpkgPath, "", -1)
stdName = filepath.Clean(stdName)
if strings.HasPrefix(stdName, "/") {
stdName = stdName[1:]
}
if strings.HasPrefix(stdName, "cmd") {
continue
}
if strings.Contains(stdName, "testdata") {
continue
}
if strings.Contains(stdName, "internal") {
continue
}
if len(stdName) < 2 {
continue
}
if _, ok := smap[stdName]; !ok {
smap[stdName] = true
}
}
return smap, nil
}
示例7: GetAPIPath
// GetAPIPath gets the Go source code path.
//
// 1. before Go 1.4: $GOROOT/src/pkg
// 2. Go 1.4 and after: $GOROOT/src
func (*mygo) GetAPIPath() string {
ret := runtime.GOROOT() + "/src/pkg" // before Go 1.4
if !File.IsExist(ret) {
ret = runtime.GOROOT() + "/src" // Go 1.4 and after
}
return filepath.FromSlash(path.Clean(ret))
}
示例8: FullGoSearchPath
// FullGoSearchPath gets the search paths for finding packages
func FullGoSearchPath() string {
allPaths := os.Getenv(GOPATHKey)
if allPaths != "" {
allPaths = strings.Join([]string{allPaths, runtime.GOROOT()}, ":")
} else {
allPaths = runtime.GOROOT()
}
return allPaths
}
示例9: generate
func generate(pkg string, deps ...*dependency) *dependency {
var wg dependency
if exclude(pkg) {
return &wg
}
wg.Add(1)
all.Add(1)
go func() {
defer wg.Done()
defer all.Done()
// Wait for dependencies to finish.
for _, d := range deps {
d.Wait()
if d.hasErrors && !*force {
fmt.Printf("--- ABORT: %s\n", pkg)
wg.hasErrors = true
return
}
}
vprintf("=== GENERATE %s\n", pkg)
args := []string{"generate"}
if *verbose {
args = append(args, "-v")
}
args = append(args, "./"+pkg)
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
w := &bytes.Buffer{}
cmd.Stderr = w
cmd.Stdout = w
if err := cmd.Run(); err != nil {
fmt.Printf("--- FAIL: %s:\n\t%v\n\tError: %v\n", pkg, indent(w), err)
hasErrors = true
wg.hasErrors = true
return
}
vprintf("=== TEST %s\n", pkg)
args[0] = "test"
cmd = exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
wt := &bytes.Buffer{}
cmd.Stderr = wt
cmd.Stdout = wt
if err := cmd.Run(); err != nil {
fmt.Printf("--- FAIL: %s:\n\t%v\n\tError: %v\n", pkg, indent(wt), err)
hasErrors = true
wg.hasErrors = true
return
}
vprintf("--- SUCCESS: %s\n\t%v\n", pkg, indent(w))
fmt.Print(wt.String())
}()
return &wg
}
示例10: pkgpath
// pkgpath returns the destination for object cached for this Package.
func pkgpath(pkg *Package) string {
importpath := filepath.FromSlash(pkg.ImportPath) + ".a"
switch {
case pkg.isCrossCompile():
return filepath.Join(pkg.Pkgdir(), importpath)
case pkg.Standard && pkg.race:
// race enabled standard lib
return filepath.Join(runtime.GOROOT(), "pkg", pkg.gotargetos+"_"+pkg.gotargetarch+"_race", importpath)
case pkg.Standard:
// standard lib
return filepath.Join(runtime.GOROOT(), "pkg", pkg.gotargetos+"_"+pkg.gotargetarch, importpath)
default:
return filepath.Join(pkg.Pkgdir(), importpath)
}
}
示例11: TestInfoWithArgs
func TestInfoWithArgs(t *testing.T) {
gb := T{T: t}
defer gb.cleanup()
gb.tempDir("src")
gb.cd(gb.tempdir)
gb.run("info", "GB_PROJECT_DIR", "GB_MISSING", "GB_GOROOT")
gb.grepStdout(`^`+regexp.QuoteMeta(gb.tempdir), "missing "+regexp.QuoteMeta(gb.tempdir))
gb.grepStdout(`^`+regexp.QuoteMeta(runtime.GOROOT()), "missing "+regexp.QuoteMeta(runtime.GOROOT()))
// second line should be empty
lines := bytes.Split(gb.stdout.Bytes(), []byte{'\n'})
if len(lines[1]) != 0 {
t.Fatal("want 0, got", len(lines[1]))
}
}
示例12: TestBug3486
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
root, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil {
t.Fatal(err)
}
lib := filepath.Join(root, "lib")
src := filepath.Join(root, "src")
seenSrc := false
filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
if err != nil {
t.Fatal(err)
}
switch pth {
case lib:
return filepath.SkipDir
case src:
seenSrc = true
}
return nil
})
if !seenSrc {
t.Fatalf("%q not seen", src)
}
}
示例13: TestBug3486
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
root, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil {
t.Fatal(err)
}
lib := filepath.Join(root, "lib")
src := filepath.Join(root, "src")
seenSrc := false
walker := fs.Walk(root)
for walker.Step() {
if walker.Err() != nil {
t.Fatal(walker.Err())
}
switch walker.Path() {
case lib:
walker.SkipDir()
case src:
seenSrc = true
}
}
if !seenSrc {
t.Fatalf("%q not seen", src)
}
}
示例14: TestEnvVars
func (suite *SerialRunnerTestSuite) TestEnvVars() {
makeEnvVarPrintBuildFile := func(path, varname string) {
fmtStatement := fmt.Sprintf(`print(os.environ['%s'], file=sys.stdout)`, varname)
suite.makeTestBuildFile(path, []string{fmtStatement})
}
type testCase struct {
path, varname, expected string
}
env := Env{
"PATH": os.Getenv("PATH"),
"GOPATH": os.Getenv("GOPATH"),
"NOMS_CHECKOUT_PATH": "/where/noms/is",
"ATTIC_CHECKOUT_PATH": "/where/attic/is",
}
tests := []testCase{}
for n, v := range env {
tc := testCase{suite.uniqueBuildFile(), n, v}
makeEnvVarPrintBuildFile(tc.path, tc.varname)
tests = append(tests, tc)
}
gorootTestCase := testCase{suite.uniqueBuildFile(), "GOROOT", runtime.GOROOT()}
makeEnvVarPrintBuildFile(gorootTestCase.path, gorootTestCase.varname)
tests = append(tests, gorootTestCase)
log := &bytes.Buffer{}
if suite.True(Serial(log, log, env, suite.dir, buildFileBasename), "Serial() should have succeeded! logs:\n%s", string(log.Bytes())) {
logText := string(log.Bytes())
for _, tc := range tests {
suite.Contains(logText, tc.expected)
}
}
}
示例15: setEnvironment
// setEnvironment assembles the configuration for gotest and its subcommands.
func setEnvironment() {
// Basic environment.
GOROOT = runtime.GOROOT()
addEnv("GOROOT", GOROOT)
GOARCH = os.Getenv("GOARCH")
if GOARCH == "" {
GOARCH = runtime.GOARCH
}
addEnv("GOARCH", GOARCH)
O = theChar[GOARCH]
if O == "" {
Fatalf("unknown architecture %s", GOARCH)
}
// Commands and their flags.
gc := os.Getenv("GC")
if gc == "" {
gc = O + "g"
}
XGC = []string{gc, "-I", "_test", "-o", "_xtest_." + O}
GC = []string{gc, "-I", "_test", "_testmain.go"}
gl := os.Getenv("GL")
if gl == "" {
gl = O + "l"
}
GL = []string{gl, "-L", "_test", "_testmain." + O}
// Silence make on Linux
addEnv("MAKEFLAGS", "")
addEnv("MAKELEVEL", "")
}