dongsong1911 2013-10-25 00:44
浏览 182
已采纳

exec.Command(“ date”)无法找到日期命令

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    out, err := exec.Command("date").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("The date is %s
", out)
}

This is a code example from the documentation for executing system commands. http://golang.org/pkg/os/exec/#example_Cmd_Output Even on the documentation site the example execute box doesn't run and has the same error: 2009/11/10 23:00:00 exec: "date": executable file not found in $PATH

On Windows I get: exec: "date": executable file not found in %PATH%

How do I get commands to work? Do I need to set a path or write out the full path of the command?

  • 写回答

2条回答 默认 最新

  • drnx3715 2013-10-25 01:20
    关注

    Sadly, that example isn't going to work for you unless you get a date.exe from somewhere (Cygwin?) and put in on your %PATH%.

    What's going on, I believe, is that date is a builtin in Powershell. It works for you because your shell is interpreting it.

    You may be able to do

    out, err := exec.Command("cmd", "/C", "date").Output()
    

    as suggested here; I don't know, I don't have a Windows machine handy.


    Sidenote:

    Get-Command date says "The term 'date' is not recognized as the name of a cmdlet"

    There are exactly two Google results for that phrase. One of them leads me to this, which helped me figure this out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog