如何在Go中禁用凭据提示以进行git clone?

I'm trying to run the equivalent of git clone in Go but I want to exit out of the command if prompted for input from stdin (ex: authentication if an ssh key hasn't been set up in the remote repository). Is there a way to do this? Right now it will simply block on input.

UPDATE: special git solution.

Since v 2.3 git supports environment setting GIT_TERMINAL_PROMPT=0 which says not to ask for credentials but fail.

func main() {
    cmd := exec.Command("git", "clone", "https://github.com/some/non-existing-repo")
    os.Setenv("GIT_SSH_COMMAND", "ssh -oBatchMode=yes")  // 
    cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=1", "GIT_SSH_COMMAND='ssh -oBatchMode=yes'")

    cmd.Stdout = os.Stdout
    err := cmd.Run()
    if err != nil {
        log.Println("Error: ", err)
    }
} 

SSH Solution:

The accepted answer will only work with http clones. If you want a way to do this with ssh the following will work:

//disables stdin prompts for username/password. If passwordless-ssh isn't configured we want to fail
os.Setenv("GIT_SSH_COMMAND", "ssh -oBatchMode=yes") 
cmd := exec.Command(name, args...)
return cmd.CombinedOutput()

man git entry for GIT_SSH_COMMAND

If either of these environment variables is set then git fetch and git push will use the specified command instead of ssh when they need to connect to a remote system. The command will be given exactly two or four arguments: the username@host (or just host) from the URL and the shell command to execute on that remote system, optionally preceded by -p (literally) and the port from the URL when it specifies something other than the default SSH port.

$GIT_SSH_COMMAND takes precedence over $GIT_SSH, and is interpreted by the shell, which allows additional arguments to be included. $GIT_SSH on the other hand must be just the path to a program (which can be a wrapper shell script, if additional arguments are needed).

Usually it is easier to configure any desired options through your personal .ssh/config file. Please consult your ssh documentation for further details.

man git entry for GIT_TERMINAL_PROMPT

If this environment variable is set to 0, git will not prompt on the terminal (e.g., when asking for HTTP authentication).

How about a natively golang solution for using git instead. This will mean that your program won't require git and you won't have issues with stdin and the git binary. Checkout this example from https://godoc.org/gopkg.in/src-d/go-git.v4. A plus is that the documentation is really good, this is from the examples page of the project. It also supports the ssh protocol. Here is a full comparison of git vs this project.

import (
    "fmt"
    "os"

    "gopkg.in/src-d/go-git.v4"
    . "gopkg.in/src-d/go-git.v4/_examples"
)

// Basic example of how to clone a repository using clone options.
func main() {
    url := "https://github.com/some/non-existing-repo"
    directory := "/foo/bar"

    // Clone the given repository to the given directory
    Info("git clone %s %s --recursive", url, directory)

    r, err := git.PlainClone(directory, false, &git.CloneOptions{
        URL:               url,
        RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
    })

    CheckIfError(err)  

}