英文:
cloning a repo with os.exec("git", "clone")
问题
运行下面的代码,我期望在访问clone
路径时,将github托管的项目username/mysuperrepo
克隆到此go项目所在的仓库中,但它不起作用。停止应用程序后,没有mysuperrepo
目录,也没有我从命令行运行git clone https://github.com/username/mysuperrepo.git
时所期望的任何文件。
问题:为什么下面的代码不会在go程序所在的目录中产生一个克隆的仓库?
func clone(w http.ResponseWriter, r *http.Request){
var repo = "https://github.com/username/mysuperrepo.git"
exec.Command("git", "clone", repo)
w.Write([]byte(repo))
}
func main(){
http.HandleFunc("/clone/", clone)
log.Fatal(http.ListenAndServe(":8080", nil))
}
英文:
Running the code below, I expected the github hosted project username/mysuperrepo
to be cloned (once I visit the clone
path) into the repo where this go project is running, but it doesn't work. After stopping the application, there's no directory for mysuperrepo
no any of the files that I would expect from running git clone https://github.com/username/mysuperrepo.git
from the command line
Question: Why wouldn't the code below produce a clone of the repo in the directory where the go program is running?
func clone(w http.ResponseWriter, r *http.Request){
var repo = "https://github.com/username/mysuperrepo.git"
exec.Command("git", "clone", repo)
w.Write([]byte(repo))
}
func main(){
http.HandleFunc("/clone/", clone)
log.Fatal(http.ListenAndServe(":8080", nil))
}
答案1
得分: 8
你需要调用Run
来实际执行该命令。
cmd := exec.Command("git", "clone", repo)
err := cmd.Run()
if err != nil {
// 出现错误
}
英文:
You need to call Run
to actually execute the command.
cmd := exec.Command("git", "clone", repo)
err := cmd.Run()
if err != nil {
// something went wrong
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论