简体   繁体   中英

What does this snippet of Go do? Is it inheritance?

I'm reading a tutorial on using Bazil/Fuse to make a file system using Go.

In the tutorial, I see the following as an example (specifically to get the Root of the file system):

var _ fs.FS = (*FS)(nil)

func (f *FS) Root() (fs.Node, fuse.Error) {
    n := &Dir{
        archive: f.archive,
    }
    return n, nil
}

The specific line I'm not understanding is the first one. What is that actually doing? I believe if I don't include that line, I can make the signature for this method whatever I want - if I do include it, I have to match their specific signature to override.

Can someone explain exactly what this line does, and when I would want to use it?

This line of code is a compile time assertion that the type *FS satisfies the fs.FS interface. The compiler reports an error if the assertion is not true:

*FS does not implement fs.FS (missing <method name> method)

The typical use of a line like this is to ensure that a type satisfies some interface and there's no other code in the package that depends on the type satisfying the interface.

The line has no effect on the execution of the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM