2013-01-07 18:51
浏览 23


What does this snippet of Go code do?

var _ interface {
    add(string) error
} = &watcher{}

I believe &watcher{} returns two things, the first is discarded and the second is assigned to ... an interface? I found the code in fswatch on Github.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongliao1949 2013-01-07 19:00

    This construct will declare a variable with an blank identifier name with type given by a type literal; an interface definition in this case. What follows is in initializer expression - a pointer to a composite literal in this case.

    Overall functionality of the snippet is to statically ensure that a *watcher satisfies the said interface as the _ variable is not materialized in any way and only any possible side effects of the initializer can be observed. Either static (as in this case) or dynamic (like e.g. calling a function which assigns at runtime to, say, some global vars, registers a handler, etc.)

    解决 无用
    打赏 举报

相关推荐 更多相似问题