github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2015/tricks/method-once.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import "sync"
     6  
     7  type LazyPrimes struct {
     8  	once   sync.Once // Guards the primes slice.
     9  	primes []int
    10  }
    11  
    12  func (p *LazyPrimes) init() {
    13  	// Populate p.primes with prime numbers.
    14  }
    15  
    16  func (p *LazyPrimes) Primes() []int {
    17  	p.once.Do(p.init)
    18  	return p.primes
    19  }