Go 无法将(类型[]字节)用作strconv.ParseFloat问题的参数中的类型字符串

Go 无法将(类型[]字节)用作strconv.ParseFloat问题的参数中的类型字符串,go,gpio,Go,Gpio,因此,我得到: pi@raspberrypi:~/Desktop/go $ go run shell1.go Go文件(shell1.Go)代码为: pi@raspberrypi:~/Desktop/go $ go run shell1.go # command-line-arguments ./shell1.go:29: undefined: n ./shell1.go:29: cannot use b (type []byte) as type string in argument to

因此,我得到:

pi@raspberrypi:~/Desktop/go $ go run shell1.go
Go文件(
shell1.Go
)代码为:

pi@raspberrypi:~/Desktop/go $ go run shell1.go
# command-line-arguments
./shell1.go:29: undefined: n
./shell1.go:29: cannot use b (type []byte) as type string in argument to strconv.ParseFloat
./shell1.go:32: undefined: n
gpio.bash
)文件的内容只是读取gpio的一个命令

package main

import (
    //    "net/http"
    //    "github.com/julienschmidt/httprouter"
    "fmt"
    "log"
    "os/exec"
    "strconv"
    "time"
    //"bytes"
    //"encoding/binary"
)
import _ "github.com/go-sql-driver/mysql"
import _ "database/sql"

func main() {
    for {
        time.Sleep(10 * time.Millisecond)
        cmd := exec.Command("gpio.bash")

        b, err := cmd.Output()
        if err != nil {
            log.Fatal(err)
        }
        n, _ = strconv.ParseFloat(b, 10)
        fmt.Println(string(b))
        break
    }

    fmt.Println("Button pressed!!! ", n)

}
您在这里使用的是一个,它当然可以执行任何操作

该函数是故意泛型的,因为真正的返回类型根据执行的内容而变化。因此,当您调用该方法时,会得到一个字节片(非常通用!)。签名如下:

 #!/bin/bash
gpio read 29
如果知道字节始终是字符串,则只需将类型转换为字符串:

func (c *Cmd) Output() ([]byte, error)

n,=strconv.ParseFloat(b,10)
更改为
n,:=strconv.ParseFloat(string(b),10)
。编译器错误具有修复此问题所需的确切信息。
n, _ := strconv.ParseFloat(string(b), 10)