Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 当作为参数传递给单独包中的函数时,Struct不是类型_Go - Fatal编程技术网

Go 当作为参数传递给单独包中的函数时,Struct不是类型

Go 当作为参数传递给单独包中的函数时,Struct不是类型,go,Go,我正在制作一个JSON验证函数,并试图实现它。但是,当我尝试将结构作为参数导入另一个包中的验证函数时,我的结构出现了问题 之所以在另一个包中,是因为我将在不同的路由文件中调用一个通用验证函数,所以我实际上不能在该包中有任何结构,它们必须在路由文件中导入和定义 type UsersJSON struct { Users struct { Put []UserJSON `json:"PUT"` } `json:"users"` } type UserJSON str

我正在制作一个JSON验证函数,并试图实现它。但是,当我尝试将结构作为参数导入另一个包中的验证函数时,我的结构出现了问题

之所以在另一个包中,是因为我将在不同的路由文件中调用一个通用验证函数,所以我实际上不能在该包中有任何结构,它们必须在路由文件中导入和定义

type UsersJSON struct {
    Users struct {
        Put []UserJSON `json:"PUT"`
    } `json:"users"`
}

type UserJSON struct {
    display_name     string `json:"display_name"`
    username        string `json:"username"`
    email           string `json:"email"`
    password        string `json:"password"`
}

func MyFunc(w http.ResponseWriter, r *http.Request) {
    errors, _ := schema.Validate(data, r, UsersJSON)
}

func Validate(schemaHelper interface{}) (interface{}, error) {
    file, err := os.Open("config/schema/schema.json")
    if err != nil {
        return nil, err
    }

    defer file.Close()

    byteValue, _ := ioutil.ReadAll(file)

    var helpers schemaHelper // this is the error

    json.Unmarshal(byteValue, &helpers)
    fmt.Printf("%v", helpers)
}
我的JSON模式如下所示:

{
    "users": {
        "PUT": {
        }
    }
}
func MyFunc(w http.ResponseWriter, r *http.Request) {
    var unmarshaled UsersJSON
    errors, _ := schema.Validate(&unmarshaled)
}

func Validate(schemaHelper interface{}) (error) {
    file, err := os.Open("config/schema/schema.json")
    if err != nil {
        return nil, err
    }

    defer file.Close()

    byteValue, _ := ioutil.ReadAll(file)

    return json.Unmarshal(byteValue, schemaHelper)
}
我想让这种方法发挥作用,因为它使自动化验证变得更容易、更快

这不会编译并产生错误
schemaHelper不是一个类型


知道我如何解决这个问题吗?

您试图声明一个名为
helpers
的变量,类型为
schemaHelper
,但是(至少在显示的代码中)您没有定义任何名为
schemaHelper
的类型。您有一个名为
schemaHelper
的变量。不能将类型用作变量,也不能将变量用作类型。您必须通过传递一个
UsersJSON
的实例来调用
Validate
,然后您可以直接将JSON解组。看起来你可能要找的是这样的东西:

{
    "users": {
        "PUT": {
        }
    }
}
func MyFunc(w http.ResponseWriter, r *http.Request) {
    var unmarshaled UsersJSON
    errors, _ := schema.Validate(&unmarshaled)
}

func Validate(schemaHelper interface{}) (error) {
    file, err := os.Open("config/schema/schema.json")
    if err != nil {
        return nil, err
    }

    defer file.Close()

    byteValue, _ := ioutil.ReadAll(file)

    return json.Unmarshal(byteValue, schemaHelper)
}
这将把JSON解组到变量
schemaHelper
(不管它是什么类型)中,以便调用者根据需要使用。请注意,这是一个粗略的最佳猜测,因为在您的问题中,
Validate
的调用传递了3个参数,但给出的函数定义只接受1个参数

然而,我不认为这会像你基于问题所认为的那样进行“验证”。它只验证JSON在语法上是否有效,而不是它与传入的
struct
有任何关系-它可能有未在struct中定义的字段,可能缺少已定义的字段,并且不会返回任何错误


最后,类型
UsersJSON
没有导出字段(所有字段都以小写字母开头,使它们未被导出/私有),因此无论发生什么情况,都不会将任何内容解组到其中
encoding/json
只能将其解组到导出的字段中。

出现此错误是因为
schemaHelper
不是类型。事实上,它是
接口{}
类型的函数变量,正如您在函数
验证
中声明的那样。谢谢您的回答,它非常有用。我只是想知道,正如在我的问题中看到的,我有一个变量,但在你的答案中它是缺失的。我需要它,这样我就可以循环json文件,等等。这还可能吗?我删除的唯一变量是
helpers
,因为它的定义是错误的。如果您想使用未编组的JSON,它位于
schemaHelper
变量中。