英文:
Why when type command to run a program, auto insert ./ at previous of program name?
问题
例如,
package main
import (
   "fmt"
    "net/http"
)
func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "你好,我叫Inigo Montoya")
}
func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe("localhost:4000", nil)
}
当输入命令时,为什么不是inigo,而是自动转换为./inigo?
英文:
For example,
package main
import (
   "fmt"
    "net/http"
)
func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "Hello, my name is Inigo Montoya")
}
func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe("localhost:4000", nil)
}
When type command , Why not inigo , auto convert to ./inigo ?
答案1
得分: 2
你需要使用以下命令:
./inigo
因为 . 不在你的 PATH 中,也不应该在其中。 . 只是指当前目录。
假设你正在使用 bash,根据问题的标签,这个替代方法也可以:
$PWD/inigo
同样,任何其他相对或绝对路径到 inigo 的方式也可以正常工作。
编辑
另外,你可以将 $PWD 添加到你的 PATH 中,这样做的好处是不需要输入上述任何命令,而且不会有添加 . 的安全隐患。
假设你在包含 inigo 的目录中,然后执行以下命令:
PATH+=:$PWD
然后你只需要在任何地方输入以下命令:
inigo
英文:
You need
./inigo
because . is not, and it should not be, in your PATH. . just refers to the current directory.
Assuming you are using bash, as the label to the question suggests, this alternative
$PWD/inigo
would also work as well as any other relative or absolute path to inigo.
edit
Alternative, you can add $PWD to your PATH which will have the advantage of not requiring you to type any of the above without having the security implications of adding ..
Let's say you are in the directory that contains inigo, then
PATH+=:$PWD
and then just type
inigo
from anywhere.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论