You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
801 B
Go
46 lines
801 B
Go
package resp
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
type GtpResponse struct {
|
|
Command string
|
|
Content string
|
|
|
|
Err error
|
|
}
|
|
|
|
func NewGtpResponse(cmd string) *GtpResponse {
|
|
return &GtpResponse{
|
|
Command: cmd,
|
|
}
|
|
}
|
|
|
|
func (g *GtpResponse) SetContent(str string) {
|
|
reg, _ := regexp.Compile(`\t`)
|
|
result := strings.TrimSpace(reg.ReplaceAllString(str, " "))
|
|
|
|
if len(result) > 0 {
|
|
first := result[0]
|
|
switch first {
|
|
case '=':
|
|
g.Content = strings.TrimSpace(result[1:])
|
|
case '?':
|
|
g.Err = errors.New(strings.TrimSpace(result[1:]))
|
|
default:
|
|
g.Err = errors.New("未能识别GTP引擎返回值")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (g *GtpResponse) String() string {
|
|
if g.Err != nil {
|
|
return fmt.Sprintf("Err: [%v]", g.Err)
|
|
}
|
|
return fmt.Sprintf("CMD:[%s] Response:[%s]", g.Command, g.Content)
|
|
}
|