无法将(类型为字符串)转换为类型errorString。

huangapple go评论86阅读模式
英文:

cannot convert (type string) to type errorString

问题

我写了一个简单的程序,执行uptime命令并获取其结果。作为错误处理的一部分,我在将错误类型转换为字符串和字符串类型转换为错误时遇到了麻烦。我该如何实现这个?

package main

import (
  "fmt"
  "os/exec"
)

type errorString struct {
    s string
  }

func (e *errorString) Error() string {
  return e.s
}

func execCommand(cmd string) (string, error) {
  if cmd == "" {
    return "", &errorString("Passed empty input")
  }

  output, err := exec.Command(cmd).Output()
  fmt.Println("Output: " ,string(output))
  fmt.Println("Error: ", err)
  if err != nil {
    fmt.Printf("Received error %q while executing the command %q", err, cmd)
    return "",err
  }
  fmt.Printf("Command executed successfully.\nOutput: %s\n",output)
  return string(output), nil
}

func main() {

  command := "uptime"
  output, err := execCommand(command)
  if err != nil {
    fmt.Errorf("Received error while executing the command\n")
  } else {
    fmt.Printf("Command %s output %s ", command, output)
  }
}

在执行时出现以下错误

agastya in uptime_cmd on   my-code-go
❯  go run execute_uptime_command_1.go
# command-line-arguments
./execute_uptime_command_1.go:18:28: cannot convert "Passed empty input" (type string) to type errorString
agastya in uptime_cmd on   my-code-go

我想要实现的是,将字符串转换为错误,反之亦然。我尝试在上述代码中实现以下测试用例

package main
import (
  "testing"
  "strings"
)

func TestExecCommand(t *testing.T) {

  command := "uptime"
  expectedIncludes := "load"
  received, err := execCommand(command)
  if !strings.Contains(received, expectedIncludes) {
    t.Errorf("Expecting %q to include %q", received, expectedIncludes)
  }

  received, err = execCommand("")
  if received != "" {
    t.Errorf("Expecting empty response when command is empty")
  }

  received, err = execCommand("uptime1")
  if !strings.Contains(string(err), "executable file not found in $PATH") {
    t.Errorf("Expecting executable not found error while executing invalid command as 'uptime1'");
  }
}

但是由于上述错误,无法继续进行。非常感谢任何建议。不需要提供明确的解决方案,参考文章也可以。

谢谢。

英文:

I wrote a simple program which does executing uptime command and fetch its result. As part of error handling I am getting trouble while converting error type into string and string type to error. How can I achieve this ?

package main

import (
  "fmt"
  "os/exec"
)

type errorString struct {
    s string
  }

func (e *errorString) Error() string {
  return e.s
}

func execCommand(cmd string) (string, error) {
  if cmd == "" {
    return "", &errorString("Passed empty input")
  }

  output, err := exec.Command(cmd).Output()
  fmt.Println("Output: " ,string(output))
  fmt.Println("Error: ", err)
  if err != nil {
    fmt.Printf("Received error %q while executing the command %q", err, cmd)
    return "",err
  }
  fmt.Printf("Command executed successfully.\nOutput: %s\n",output)
  return string(output), nil
}

func main() {

  command := "uptime"
  output, err := execCommand(command)
  if err != nil {
    fmt.Errorf("Received error while executing the command\n")
  } else {
    fmt.Printf("Command %s output %s ", command, output)
  }
}

And while executing getting below error

agastya in uptime_cmd on   my-code-go
❯  go run execute_uptime_command_1.go
# command-line-arguments
./execute_uptime_command_1.go:18:28: cannot convert "Passed empty input" (type string) to type errorString
agastya in uptime_cmd on   my-code-go

What I am trying to achieve is, trying to convert a string into error and vice versa. I am trying to implement below test cases to above code

package main
import (
  "testing"
  "strings"
)

func TestExecCommand(t *testing.T) {

  command := "uptime"
  expectedIncludes := "load"
  received, err := execCommand(command)
  if !strings.Contains(received, expectedIncludes) {
    t.Errorf("Expecting %q to include %q", received, expectedIncludes)
  }

  received, err = execCommand("")
  if received != "" {
    t.Errorf("Expecting empty response when command is empty")
  }

  received, err = execCommand("uptime1")
  if !strings.Contains(string(err), "executable file not found in $PATH") {
    t.Errorf("Expecting executable not found error while executing invalid command as 'uptime1'");
  }
}

And Unable to proceed with above error. Any suggestions much appreicated. It dont have to be explicit solution, even reference articles are fine.

Thank you.

答案1

得分: 1

创建一个错误字符串的方法是:

return "", &errorString{"传入了空输入"}
英文:

To create an error from string use

return "", &errorString{"Passed empty input"}

huangapple
  • 本文由 发表于 2022年1月31日 18:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/70924484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定