我有要验证的字段的简单 struct .稍后,这将是一个嵌套得很深的 struct ,因为现在它很简单.
type SpotRequest struct { //holder struct
Location Location
}
type Location struct { // target struc, this is going to be validated
Longitude float64
Latitude float64
}
type Error struct { //return to caller of a service
Field string
Reason string
}
这是一个异步函数(缺少更好的词):
func validateLocation(location *Location, ch chan []Error) {
var errors []Error
//some custom logic which will return list of errors
errr := &Error{
Field: "myField",
Reason: "value must start with upper case",
}
ch <- append(s, *errr)
}
主代码:
ch := make(chan []Error, 1) //I have set for the second argument value of 1 since I will be returning single list with zero or more Error structs
go validateLocation(&spotRequest.Location, ch)
for i := 0; i < 1; i++ { //iterating one time since I need to get one value from channel ch
select {
case msg1 := <-ch:
fmt.Printf("Result from location validations: %v \n", msg1)
default:
fmt.Println("Should not print")
}
}
流只在DEFAULT子句中结束.我哪里错了?第一次和大猩猩在一起.