3

I was reading DigitalOcean's golang client. I noticed that they create an instance of their *Op struct in a _ variable. Example: https://github.com/digitalocean/godo/blob/master/droplets.go#L32

var _ DropletsService = &DropletsServiceOp{}

Why is this line needed?

codefx
  • 9,872
  • 16
  • 53
  • 81

2 Answers2

10

This line is a compile time check that *DropletsServiceOp satisfies the DropletsService interface.

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

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
3

If you look at the blame on that file, at that line, you get a clue:

https://github.com/digitalocean/godo/blame/master/droplets.go#L32

It does a compile-time check that *DropletsServiceOp satisfies the DropletsService interface. Prior to the commit introducing that, they were doing this in their test suite.

Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64