package main
import (
//You may need to edit these in or out depending upon the attempt
"bytes"
"encoding/binary"
"fmt"
//"strconv"
//"math"
)
func main() {
// I have been able to test this part and I do get a return of "[50 10]" which is correct (converts string "2" for 2 CPUs). The exact statement may be a bit different.
// I am running the script on the latest FreeNAS whic his built from 11.1STABLE FreeBSD. I have Go installed and am building the files on the shell
//numCPUCmd := exec.Command("bash", "-c","/usr/local/bin/ipmitool -I lanplus -H ipmiAddress -U ipmiUser -f /root/ipmi_password sdr elist all | grep -c -i \"cpu.*temp\"")
//numCPUBytes, _ := numCPUCmd.Output() //returns a slice of bytes and an error
// ATTEMPT 1
var flty float64
sob := []byte{50, 10}
fmt.Printf("\n%T %v\n\n", sob, sob)
buf := bytes.NewReader(sob)
err := binary.Read(buf, binary.LittleEndian, &flty)
if err != nil {
fmt.Println("binary.Read failed:", err)
}
fmt.Println(flty)
/*Result
[]uint8 [50 10]
binary.Read failed: unexpected EOF
0
*/
////////////////////////////////////////////////////////
//ATTEMPT 2
/*
var f float64
text := []byte{50, 10} // A decimal value represented as Latin-1 text
f, err := strconv.ParseFloat(string(text), 64)
if err != nil {
panic(err)
}
fmt.Println(f)
*/
/*Result
panic: strconv.ParseFloat: parsing "2\n": invalid syntax
goroutine 1 [running]:
main.main()
/tmp/sandbox657430918/main.go:44 +0x160
*/
////////////////////////////////////////////////
//ATTEMPT3
/*
sob := []byte{50, 10}
bits := binary.LittleEndian.Uint64(sob)
fmt.Printf("\n\n%T %v\n\n", bits, bits)
flty := math.Float64frombits(bits)
fmt.Printf("\n\n%T %v\n\n", flty, flty)
inty := int(flty)
fmt.Printf("\n\n%T %v\n\n", inty, inty)
*/
/* Result
panic: runtime error: index out of range
goroutine 1 [running]:
encoding/binary.binary.littleEndian.Uint64(...)
/usr/local/go/src/encoding/binary/binary.go:76
main.main()
/tmp/sandbox742704811/main.go:62 +0x20
*/
}For production use, leave out the grep and bash, read the output lines directly with bufio.Scanner.
func (c *Cmd) Output() ([]byte, error) { if c.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
}
var stdout bytes.Buffer
c.Stdout = &stdout
captureErr := c.Stderr == nil
if captureErr {
c.Stderr = &prefixSuffixSaver{N: 32 << 10}
}
err := c.Run()
if err != nil && captureErr {
if ee, ok := err.(*ExitError); ok {
ee.Stderr = c.Stderr.(*prefixSuffixSaver).Bytes()
}
}
return stdout.Bytes(), err
}
stdout.Bytes() = bytes.Buffer.Bytes()
bytes/buffer.go
// Bytes returns a slice of length b.Len() holding the unread portion of the buffer. // The slice is valid for use only until the next buffer modification (that is, // only until the next call to a method like Read, Write, Reset, or Truncate). // The slice aliases the buffer content at least until the next buffer modification, // so immediate changes to the slice will affect the result of future reads. func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
// A Buffer is a variable-sized buffer of bytes with Read and Write methods. // The zero value for Buffer is an empty buffer ready to use. type Buffer struct { buf []byte // contents are the bytes buf[off : len(buf)] off int // read at &buf[off], write at &buf[len(buf)] bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation. lastRead readOp // last read operation, so that Unread* can work correctly. // FIXME: it would be advisable to align Buffer to cachelines to avoid false // sharing. }
I am still not sure where the "10" or '\n' came from, but I know enough to get the program working. I am really enjoying getting to learn this language and excited for all that I can get it to do, even if it comes with the expected frustrations and head against wall banging.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.