英文:
Showing the error after executing the entire code
问题
我有一个循环,在循环中对值进行排序,并比较这些值。如果存在相同的值,则从相同的值数组中删除它们。之后,我想要做以下操作,以便代码继续运行直到结束。然后,通知用户这些被删除的值没有被添加。但是我写的代码导致进一步的代码执行停止。告诉我如何修改代码,使得即使遇到相同的值,代码也能一直运行到最后,然后再输出关于这些相同值的消息。
另外:我以json的结构给出了没有重复的信息。但我还想提供错误信息。告诉我如何正确实现这一点。
type Result struct {
Result int `json:"result"`
Message string `json:"message"`
Status []string
}
var result Result
var searchProject = make([]struct{
ProjectId string
}, 0)
var query string
switch Type {
case sku:
query = fmt.Sprintf(`
SELECT s.project_id
FROM sku.sku_projects s
WHERE s.deleted_at IS NULL
AND s.sku_id = %d
AND s.project_id IN (%s)
`, val.FieldByName("SkuID").Int(), ProjectId)
case user:
query = fmt.Sprintf(`
SELECT u.project_id
FROM users.user_project u
WHERE u.deleted_at IS NULL
AND u.user_id = %d
AND u.project_id IN (%s)
`, val.FieldByName("UserID").Int(), ProjectId)
case reason:
query = fmt.Sprintf(`
SELECT r.project_id
FROM reasons.reason_projects r
WHERE r.deleted_at IS NULL
AND r.reason_id = %d
AND r.project_id IN (%s)
`, val.FieldByName("ReasonID").Int(), ProjectId)
default:
http.NotFound(w, r)
return
}
_ = DB.Raw(query).Scan(&searchProject)
key := make([]int, 0)
double := make([]string, 0)
if len(searchProject) > 0{
for _, v := range searchProject{
for i, value := range Projects{
if value == v.ProjectId{
key = append(key, i)
double = append(double, value)
}
}
}
for _, v := range key{
Projects = append(Projects[:v], Projects[v+1:]...)
}
}
if len(key) > 0{
if (len(Projects) != len(searchProject)) {
http.Error(w, ("You are connecting already existing bindings"), http.StatusBadRequest)
return
}else {
result.Result = 0
result.Message = "The following project bindings already exist:"
result.Status = double
utils.WriteJSON(w, result)
}
}
utils.WriteJSON(w, &Struct)
}
}
英文:
I have a loop in which the values are sorted, as well as the comparison of these values. If there are identical values, they are removed from the same array of values. After that, I want to do the following so that the code continues to run until the end. And after that, inform users that these values (which were deleted) were not added. But I wrote the code, so this causes the execution of further code to stop. Tell me how to make it right so that even if the same values meet, the code works to the end and only then output messages about these identical values.
In addition: I give the structure in json without duplicates. But I also want to give information that errors have occurred. Tell me how to implement this moment correctly
type Result struct {
Result int `json:"result"`
Message string `json:"message"`
Status []string
}
var result Result
var searchProject = make([]struct{
ProjectId string
}, 0)
var query string
switch Type {
case sku:
query = fmt.Sprintf(`
SELECT s.project_id
FROM sku.sku_projects s
WHERE s.deleted_at IS NULL
AND s.sku_id = %d
AND s.project_id IN (%s)
`, val.FieldByName("SkuID").Int(), ProjectId)
case user:
query = fmt.Sprintf(`
SELECT u.project_id
FROM users.user_project u
WHERE u.deleted_at IS NULL
AND u.user_id = %d
AND u.project_id IN (%s)
`, val.FieldByName("UserID").Int(), ProjectId)
case reason:
query = fmt.Sprintf(`
SELECT r.project_id
FROM reasons.reason_projects r
WHERE r.deleted_at IS NULL
AND r.reason_id = %d
AND r.project_id IN (%s)
`, val.FieldByName("ReasonID").Int(), ProjectId)
default:
http.NotFound(w, r)
return
}
_ = DB.Raw(query).Scan(&searchProject)
key := make([]int, 0)
double := make([]string, 0)
if len(searchProject) > 0{
for _, v := range searchProject{
for i, value := range Projects{
if value == v.ProjectId{
key = append(key, i)
double = append(double, value)
}
}
}
for _, v := range key{
Projects = append(Projects[:v], Projects[v+1:]...)
}
}
if len(key) > 0{
if (len(Projects) != len(searchProject)) {
http.Error(w, ("You are connecting already existing bindings"), http.StatusBadRequest)
return
}else {
result.Result = 0
result.Message = "The following project bindings already exist:"
result.Status = double
utils.WriteJSON(w, result)
}
}
utils.WriteJSON(w, &Struct)
}
}
</details>
# 答案1
**得分**: 1
尝试使用来自 hashicorp 的 [这个库](https://github.com/hashicorp/go-multierror)。基本上,你需要将所有的错误收集到一个数组中,然后返回它。
<details>
<summary>英文:</summary>
Try to use [this library](https://github.com/hashicorp/go-multierror) from hashicorp. Basically you need to collect all the errors into an array and then return it
</details>
# 答案2
**得分**: 0
使用[multierr][1]
```golang
var err error
// ... 你的逻辑
// 假设你想要报错的地方
if (len(Projects) != len(searchProject)) {
err = multierr.Append(err, errors.New("你的新错误消息"))
}
// 当你返回一个回复时
http.Error(w, err.Error(), http.StatusBadRequest)
英文:
Using multierr
var err error
// ... your logic
// where I assume you want to error
if (len(Projects) != len(searchProject)) {
err = multierr.Append(err, errors.New("you new error message"))
}
// when you return a reply
http.Error(w, err.Error(), http.StatusBadRequest)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论