Replies: 7 comments 7 replies
-
os/execPackage exec runs external commands. It wraps os.StartProcess to make it easier to remap stdin and stdout, connect I/O with pipes, and do other adjustments. go os/exec 简明教程
Go 1.19变化从Go 1.19开始,exec包定义了变量: var ErrDot = errors.New("cannot run executable found relative to current directory")
假定你坚持要使用相对地址命令 😂
命令搜索逻辑源码
func Command(name string, arg ...string) *Cmd {
cmd := &Cmd{
Path: name,
Args: append([]string{name}, arg...),
}
if filepath.Base(name) == name {
lp, err := LookPath(name)
if lp != "" {
// Update cmd.Path even if err is non-nil.
// If err is ErrDot (especially on Windows), lp may include a resolved
// extension (like .exe or .bat) that should be preserved.
cmd.Path = lp
}
if err != nil {
cmd.Err = err
}
}
return cmd
} 所以,我们要搞清楚命令搜索逻辑主要是搞清楚 func LookPath(file string) (string, error) {
// NOTE(rsc): I wish we could use the Plan 9 behavior here
// (only bypass the path if file begins with / or ./ or ../)
// but that would not match all the Unix shells.
if strings.Contains(file, "/") {
err := findExecutable(file)
if err == nil {
return file, nil
}
return "", &Error{file, err}
}
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = "."
}
path := filepath.Join(dir, file)
if err := findExecutable(path); err == nil {
if !filepath.IsAbs(path) && godebug.Get("execerrdot") != "0" {
return path, &Error{file, ErrDot}
}
return path, nil
}
}
return "", &Error{file, ErrNotFound}
} 逻辑图Cmd不能重用Cmd的数据结构,代表了一个执行的外部命令,主要调用它的Run、Output、CombinedOutput方法后这个对象就不能重用了
|
Beta Was this translation helpful? Give feedback.
-
encoding/jsonPackage json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions. omitemptyThe "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.
// Field appears in JSON as key "myName".
Field int `json:"myName"`
// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`
// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"` -As a special case, if the field tag is "-", the field is always omitted. Note that a field with name "-" can still be generated using the tag "-,".
// Field is ignored by this package.
Field int `json:"-"`
// Field appears in JSON as key "-".
Field int `json:"-,"` |
Beta Was this translation helpful? Give feedback.
-
net/http常量变量方法
|
Beta Was this translation helpful? Give feedback.
-
golang.org/x/modThis repository holds packages for writing tools that work directly with Go module mechanics. That is, it is for direct manipulation of Go modules themselves.
|
Beta Was this translation helpful? Give feedback.
-
标准库学习
Beta Was this translation helpful? Give feedback.
All reactions