2016-09-20 98 views
2

需要检查,如果被传递到FUNC参数是零,返回0如何检查传入函数的参数是否为Go?

下面是我的意图代码

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) 

我应该改变的参数为 “输入* bean.Request”?

+1

这里的问题是问题描述本身:“检查传递给func的参数是否为零”。 *可以是零的唯一的东西是指针,接口和片/地图/频道。如果'bean.Request'不是他们中的任何一个,那么问题本身就没有形成。重新确定问题是明确哪些条件要检查'bean.Request';无可救药, – Volker

回答

2

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

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 
} 

你有一些选项(根据您的使用情况,请参见:Pointers vs. values in parameters and return values):

1 - 您可以使用指针(input *bean.Request),并将其与nil
比较2-您可以使用另一个结构并将其与reflect.DeepEqual(r, zero)
比较3- Y OU可以编写自己的compare功能(或方法与指针或值接收器)

看到这个样本(试试The Go Playground):

package main 

import (
    "fmt" 
    "reflect" 
) 

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

func main() { 
    var input *Request 
    if input == nil { 
     fmt.Println("input is nil.") //input is nil. 
    } 

    input = &Request{} 
    if input != nil { 
     fmt.Println("input is not nil.") //input is not nil. 
    } 
    r := Request{} 
    fmt.Printf("Zero value: %#v\n", r) //Zero value: main.Request{I:0} 

    zero := Request{} 
    fmt.Println("r == zero :", r == zero) //r == zero : true 

    fmt.Println("DeepEqual :", reflect.DeepEqual(r, zero)) //DeepEqual : true 
    fmt.Println("compare :", compare(&r, &zero))   //compare : true 

} 
func compare(r, zero *Request) bool { 
    return r.I == zero.I 
} 

type Request struct { 
    I int 
} 
type Transaction struct{} 

输出:

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

Comparison operators

4-您可以将其与z ERO值(零为指针,如果是struct它的零值是空的结构,如果是喜欢struct{}(不nil),或结构用初始化为它们的零个值的所有字段):

The zero value

当通过声明 或通过调用新函数或创建新值(通过 复合文字或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”:How to compare struct, slice, map are equal?

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. 
0

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