Questo è retta dalla Andrew Gerrand di 10 things you (probably) don't know about Go:
var fs fileSystem = osFS{}
type fileSystem interface {
Open(name string) (file, error)
Stat(name string) (os.FileInfo, error)
}
type file interface {
io.Closer
io.Reader
io.ReaderAt
io.Seeker
Stat() (os.FileInfo, error)
}
// osFS implements fileSystem using the local disk.
type osFS struct{}
func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
Per far funzionare tutto questo, è necessario scrivere il codice per prendere un argomento fileSystem
(forse incorporare in qualche altro tipo, o lasciare che nil
indicano il file system predefinito).
fonte
2013-05-24 19:43:54
Sembra promettente, ma per quanto riguarda le chiamate dirette a 'os.Open'? sta assegnando la variabile 'fs' a" silenziosamente "influenzarli? – canni
@canni: no, non influenzerà le chiamate dirette. Se è quello che vuoi, penso che dovrai fare scherzi al linker, e non l'ho mai provato da solo. –
Mi piacerebbe vedere un esempio di codice funzionante. – Paradiesstaub