Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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中是否为nil?_Go - Fatal编程技术网

如何检查传入函数的参数在Go中是否为nil?

如何检查传入函数的参数在Go中是否为nil?,go,Go,需要检查传递给func的参数是否为nil并返回0 下面是我想要的代码 func (t *Transaction) GetOperationCount(input bean.Request) (int, error) { var result int = 0 if input == nil { //error here return result, nil } // Other code here!!!! return result, nil

需要检查传递给func的参数是否为nil并返回0

下面是我想要的代码

func (t *Transaction) GetOperationCount(input bean.Request) (int, error) {
    var result int = 0
    if input == nil { //error here
        return result, nil
    }
    // Other code here!!!!
    return result, nil
}
bean.Reques
是一个结构。 但它有一个问题:“无法将nil转换为bean.Request输入请求类型”。我一直在努力

if (bean.Request{})== input
但它给出了:

"json:\"MV_STATUS\""; NDFNFFP string "json:\"NDF_NFFP\""; NDFNFMV string "json:\"NDF_NFMV\"" } "json:\"attr\"" } "json:\"marke
t_value\"" } "json:\"market_values\"" } "json:\"tick\"" } "json:\"insertion\"" } "json:\"operation\"" } "json:\"transaction\"" 
} cannot be compared)

我是否应该将参数更改为“input*bean.Request”?

简短回答:是的,这是工作版本:

func (t *Transaction) GetOperationCount(input *bean.Request) (int, error) {
    var result int = 0
    if input == nil {
        return result, nil
    }
    // Other code here
    return result, nil
}

您有一些选项(取决于您的用例,请参阅:):

1-您可以使用指针(
input*bean.Request
)并将其与
nil

2-您可以使用另一个结构并将其与
reflect.DeepEqual(r,zero)

3-您可以编写自己的
比较
函数(或带有指针或值接收器的方法)

请参见此示例(试穿):

输出:

input is nil.
input is not nil.
Zero value: main.Request{I:0}
r == zero : true
DeepEqual : true
compare   : true

:

4-您可以将其与其零值进行比较(指针为nil,如果它是
struct
,则其零值为空struct,如果它像
struct{}
(而不是
nil
),或者所有字段都初始化为零值的struct):

:

当通过声明为变量分配存储时 或者调用new,或者当通过 复合文字或make调用,不需要显式初始化 如果提供,则为变量或值提供默认值。每个元素 此类变量或值的类型设置为零值: 布尔为false,整数为0,浮点为0.0,字符串为“”, 指针、函数、接口、片、通道和 地图。此初始化是递归完成的,因此每个实例 如果没有值,结构数组的元素的字段将为零 是指定的。 这两个简单的声明是等价的:

var i int
var i int = 0
之后

type T struct { i int; f float64; next *T }
t := new(T)
以下观点认为:

t.i == 0
t.f == 0.0
t.next == nil
同样的情况也会发生

var t T

参见“reflect.DeepEqual”:

文件:


是的..错误本身提到它不能同时比较两者。您可以使用指针与nil进行比较,也可以创建一个空结构进行比较。

这里的问题是问题描述本身:“检查传递给func的参数是否为nil”。唯一可以为零的是指针、接口和切片/映射/通道。如果
bean.Request
都不是它们,那么问题本身的格式就不好。将问题重新表述为明确的
bean.Request上要检查的条件;零是不可能的,
var t T
func DeepEqual(x, y interface{}) bool
 DeepEqual reports whether x and y are ``deeply equal,'' defined as follows.
 Two values of identical type are deeply equal if one of the following cases applies.
 Values of distinct types are never deeply equal.

 Array values are deeply equal when their corresponding elements are deeply equal.

 Struct values are deeply equal if their corresponding fields,
 both exported and unexported, are deeply equal.

 Func values are deeply equal if both are nil; otherwise they are not deeply equal.

 Interface values are deeply equal if they hold deeply equal concrete values.

 Map values are deeply equal if they are the same map object
 or if they have the same length and their corresponding keys
 (matched using Go equality) map to deeply equal values.

 Pointer values are deeply equal if they are equal using Go's == operator
 or if they point to deeply equal values.

 Slice values are deeply equal when all of the following are true:
 they are both nil or both non-nil, they have the same length,
 and either they point to the same initial entry of the same underlying array
 (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal.
 Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil))
 are not deeply equal.

 Other values - numbers, bools, strings, and channels - are deeply equal
 if they are equal using Go's == operator.

 In general DeepEqual is a recursive relaxation of Go's == operator.
 However, this idea is impossible to implement without some inconsistency.
 Specifically, it is possible for a value to be unequal to itself,
 either because it is of func type (uncomparable in general)
 or because it is a floating-point NaN value (not equal to itself in floating-point comparison),
 or because it is an array, struct, or interface containing
 such a value.
 On the other hand, pointer values are always equal to themselves,
 even if they point at or contain such problematic values,
 because they compare equal using Go's == operator, and that
 is a sufficient condition to be deeply equal, regardless of content.
 DeepEqual has been defined so that the same short-cut applies
 to slices and maps: if x and y are the same slice or the same map,
 they are deeply equal regardless of content.