douxi6903 2016-10-23 02:48
浏览 59
已采纳

Go的这段代码有什么作用? 是继承吗?

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?

  • 写回答

1条回答 默认 最新

  • dongnaoben4456 2016-10-23 03:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件