簡短的回答:是的,這裏是工作的版本:
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.
這裏的問題是問題描述本身:「檢查傳遞給func的參數是否爲零」。 *可以是零的唯一的東西是指針,接口和片/地圖/頻道。如果'bean.Request'不是他們中的任何一個,那麼問題本身就沒有形成。重新確定問題是明確哪些條件要檢查'bean.Request';無可救藥, – Volker