나는 yaml 설정 파일에 지정할 Go 파일을 실행하고 바이트 단위의 구조체를 보내고 싶다. 내가 어떻게 할 수 있니?Go Stdin Stdout communication
client.Subscribe(NewTopic(a), func(c *Client, msg Message) {
cmd := exec.Command(v.Action)
// I actually want to send [msg] to it so it can be used there
cmd.Stdin = bytes.NewReader(msg.Bytes())
if err := cmd.Start(); err != nil {
c.Logger.Infof("Error while executing action: %v", err)
} else {
c.Logger.Info("Executed command")
}
// I want to handle responses from the called binary
var out bytes.Buffer
cmd.Stdout = &out
c.Logger.Infof("Response: %v", out)
})
I :이 내 코드입니다
subscribers:
temp:
topics:
- pi/+/temp
action: ./temp/tempBinary
:
는 나는이에 대한 표준 입력과 표준 출력을 사용할 수 그러나 YAML의 설정을 알아낼 수 없다는 생각 얼마나 정확하게 이것을 할 수 있는지 알 수 없습니다.
그래서 Message.Bytes의 구현을 찾고 있습니까? 모든 메시지를 나타낼 수있는 인코딩이 가능합니다. [인코딩/* 패키지] (https://golang.org/pkg/encoding/#pkg-subdirectories)를 확인하십시오. 인기있는 선택은 encoding/json과 encoding/gob입니다. – Peter
두 번째 생각에서 인코딩/gob은 모든 동작이 Go 프로그램 인 경우에만 유용합니다. – Peter