From 254a4ede9501afb33bcfc643310f57217e8f8b36 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 20 Dec 2017 18:09:31 +0200 Subject: [PATCH] add explanation why the 'globalVisitorsController' example is a Singleton, because it was not clear for new gophers previously. Former-commit-id: 9267c415e53eeb00adadf6fde9e0aeeb415cf24a --- _examples/mvc/singleton/main.go | 13 ++++++++----- mvc/di/reflect.go | 5 ++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/_examples/mvc/singleton/main.go b/_examples/mvc/singleton/main.go index 6c31cb49..44a602bf 100644 --- a/_examples/mvc/singleton/main.go +++ b/_examples/mvc/singleton/main.go @@ -10,7 +10,7 @@ import ( func main() { app := iris.New() - mvc.New(app.Party("/")).Register(new(globalVisitorsController)) + mvc.New(app.Party("/")).Register(&globalVisitorsController{visits: 0}) // http://localhost:8080 app.Run(iris.Addr(":8080")) @@ -18,10 +18,13 @@ func main() { type globalVisitorsController struct { // When a singleton controller is used then concurent safe access is up to the developers, because - // all clients share the same controller instance instead. Note that any controller's methods - // are per-client, but the struct's field can be shared accross multiple clients if the structure - // does not have any dynamic struct field depenendies that depend on the iris.Context, - // this declares a Singleton, note that you don't have to write a single line of code to do this, Iris is smart enough. + // all clients share the same controller instance instead. + // Note that any controller's methods + // are per-client, but the struct's field can be shared across multiple clients if the structure + // does not have any dynamic struct field depenendies that depend on the iris.Context + // and ALL field's values are NOT zero, at this case we use uint64 which it's no zero (even if we didn't set it + // manually ease-of-understand reasons) because it's a value of &{0}. + // All the above declares a Singleton, note that you don't have to write a single line of code to do this, Iris is smart enough. // // see `Get`. visits uint64 diff --git a/mvc/di/reflect.go b/mvc/di/reflect.go index a0674a2a..eb4644b8 100644 --- a/mvc/di/reflect.go +++ b/mvc/di/reflect.go @@ -4,8 +4,11 @@ import "reflect" var emptyIn = []reflect.Value{} -// IsZero returns true if a value is nil, remember boolean's false is zero. +// IsZero returns true if a value is nil. // Remember; fields to be checked should be exported otherwise it returns false. +// Notes for users: +// Boolean's zero value is false, even if not set-ed. +// UintXX are not zero on 0 because they are pointers to. func IsZero(v reflect.Value) bool { switch v.Kind() { case reflect.Struct: