10 examples of 'zero value' in Go

Every line of 'zero value' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1012func (t *StructType) ZeroValue() string { return fmt.Sprintf("%s{}", t) }
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

151func Zero(value *big.Int) bool {
152 if value == nil || value.Sign() == 0 {
153 return true
154 }
155 return false
156}
257func Zero(data []byte) {
258 for i := 0; i < len(data); i++ {
259 data[i] = 0
260 }
261}
540func positiveZeroValue() Value {
541 return Value{valueNumber, __PositiveZero__}
542}
89func ZeroValue(t ValueType) interface{} {
90 switch t {
91 case TFloat:
92 return float64(0)
93 case TInt:
94 return int64(0)
95 case TString:
96 return ""
97 case TBool:
98 return false
99 case TRegex:
100 return (*regexp.Regexp)(nil)
101 case TTime:
102 return time.Time{}
103 case TDuration:
104 return time.Duration(0)
105 case TLambda:
106 return (*LambdaNode)(nil)
107 case TList:
108 return []interface{}(nil)
109 case TStar:
110 return (*StarNode)(nil)
111 case TMissing:
112 return (*Missing)(nil)
113 default:
114 return errors.New("invalid type")
115 }
116}
9func zero(x uint64) uint64 {
10 if x == 0 {
11 return 1
12 }
13 return 0
14 // branchless:
15 // return ((x>>1 | x&1) - 1) >> 63
16}
279func (gen *Generator) ValueZero(t *types.Type) string {
280 rt := gen.R.Resolve(t, gen.Package)
281 k := rt.Kind()
282 switch k {
283 case types.BlobKind:
284 return fmt.Sprintf("%sNewEmptyBlob()", gen.TypesPackage)
285 case types.BoolKind:
286 return fmt.Sprintf("%sBool(false)", gen.TypesPackage)
287 case types.NumberKind:
288 return fmt.Sprintf("%s%s(0)", gen.TypesPackage, kindToString(k))
289 case types.ListKind, types.MapKind, types.RefKind, types.SetKind:
290 return gen.UserZero(t)
291 case types.PackageKind:
292 return fmt.Sprintf("%sNewPackage()", gen.TypesPackage)
293 case types.StringKind:
294 return fmt.Sprintf(`%sNewString("")`, gen.TypesPackage)
295 case types.StructKind:
296 return fmt.Sprintf("New%s()", gen.UserName(rt))
297 case types.ValueKind:
298 // TODO: Use nil here
299 return fmt.Sprintf("%sBool(false)", gen.TypesPackage)
300 case types.TypeKind:
301 return fmt.Sprintf("%sType{R: ref.Ref{}}", gen.TypesPackage)
302 }
303 panic("unreachable")
304}
46func zero() *big.Int { return big.NewInt(0) }
59func (s U8Slice) ZeroMoreThan(i int) bool { return false }
82func (c *Conv) InitZero(cr anyvec.Creator) {
83 filterSize := c.FilterWidth * c.FilterHeight * c.InputDepth
84 c.Filters = anydiff.NewVar(cr.MakeVector(filterSize * c.FilterCount))
85 c.Biases = anydiff.NewVar(cr.MakeVector(c.FilterCount))
86 c.Conver = CurrentConverMaker()(*c)
87}

Related snippets