{"id":825,"date":"2019-12-10T19:33:05","date_gmt":"2019-12-10T19:33:05","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=825"},"modified":"2019-12-10T19:33:18","modified_gmt":"2019-12-10T19:33:18","slug":"empty-struct-in-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/empty-struct-in-go\/","title":{"rendered":"Empty struct in GO"},"content":{"rendered":"\n
Empty struct struct{} <\/strong>in GO doesn’t occupy any memory. It is of zero byte. <\/p>\n\n\n\n Below are some of the uses of empty struct<\/p>\n\n\n\n Eg. One example is in while implementing Null Object Design Pattern in Go. The Null object doesn’t have any data. See an example of Null Object Design Pattern in GO – https:\/\/golangbyexamples.com\/null-object-design-pattern-golang\/<\/a> Eg: In context package of GO we have cancelCtx<\/strong> which is represented as below. See done channel is using an empty struct as it is only used for notifying cancellation and has no data value.<\/p>\n\n\n\n
<\/p>\n\n\n\ntype cancelCtx struct {\n Context\n mu sync.Mutex \/\/ protects following fields\n done chan struct{} \/\/ created lazily, closed by first cancel call\n children map[canceler]struct{} \/\/ set to nil by the first cancel call\n err error \/\/ set to non-nil by the first cancel call\n}<\/code><\/pre>\n\n\n\n