github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/cmd/go/internal/vet/vet.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package vet implements the ``go vet'' command. 6 package vet 7 8 import ( 9 "path/filepath" 10 11 "cmd/go/internal/base" 12 "cmd/go/internal/cfg" 13 "cmd/go/internal/load" 14 "cmd/go/internal/str" 15 ) 16 17 var CmdVet = &base.Command{ 18 Run: runVet, 19 CustomFlags: true, 20 UsageLine: "vet [-n] [-x] [build flags] [vet flags] [packages]", 21 Short: "run go tool vet on packages", 22 Long: ` 23 Vet runs the Go vet command on the packages named by the import paths. 24 25 For more about vet and its flags, see 'go doc cmd/vet'. 26 For more about specifying packages, see 'go help packages'. 27 28 The -n flag prints commands that would be executed. 29 The -x flag prints commands as they are executed. 30 31 The build flags supported by go vet are those that control package resolution 32 and execution, such as -n, -x, -v, -tags, and -toolexec. 33 For more about these flags, see 'go help build'. 34 35 See also: go fmt, go fix. 36 `, 37 } 38 39 func runVet(cmd *base.Command, args []string) { 40 vetFlags, packages := vetFlags(args) 41 for _, p := range load.Packages(packages) { 42 // Vet expects to be given a set of files all from the same package. 43 // Run once for package p and once for package p_test. 44 if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles) > 0 { 45 runVetFiles(p, vetFlags, str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.SFiles)) 46 } 47 if len(p.XTestGoFiles) > 0 { 48 runVetFiles(p, vetFlags, str.StringList(p.XTestGoFiles)) 49 } 50 } 51 } 52 53 func runVetFiles(p *load.Package, flags, files []string) { 54 for i := range files { 55 files[i] = filepath.Join(p.Dir, files[i]) 56 } 57 base.Run(cfg.BuildToolexec, base.Tool("vet"), flags, base.RelPaths(files)) 58 }