By dilfish


2012-11-02 11:25:01 8 Comments

From http://golang.org/src/pkg/database/sql/driver/types.go:

type ValueConverter interface {
    // ConvertValue converts a value to a driver Value.
    ConvertValue(v interface{}) (Value, error)
}

var Bool boolType

type boolType struct{}

var _ ValueConverter = boolType{} // line 58

func (boolType) String() string { return "Bool" }

func (boolType) ConvertValue(src interface{}) (Value, error) {....}

I known that ValueConverter is an interface name. Line 58 seems to declare that boolType implement interface ValueConverter, but is that necessary? I deleted line 58 and the code works well.

2 comments

@val 2012-11-02 11:47:44

It seems like you are creating a dummy value of type ValueConverter, assigning a new boolType object to it and then discarding it (which is the meaning of the underscore in go, as in for _, elt := range myRange { ...} if you are not interested in the index of the enumeration).

My guess is that it simply correspond to a static check to ensure that the struct boolType does implement the ValueConverter interface. This way, when you change the implementation of boolType, the compiler will complain early if you broke the implementation of ValueConverter interface as it will be unable to cast your new boolType to this interface.

@zzzz 2012-11-02 11:46:52

It provides a static (compile time) check that boolType satisfies the ValueConverter interface. The _ used as a name of the variable tells the compiler to effectively discard the RHS value, but to type-check it and evaluate it if it has any side effects, but the anonymous variable per se doesn't take any process space.

It is a handy construct when developing and the method set of an interface and/or the methods implemented by a type are frequently changed. The construct serves as a guard against forgetting to match the method sets of a type and of an interface where the intent is to have them compatible. It effectively prevents to go install a broken (intermediate) version with such omission.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

3 Answered Questions

[SOLVED] What does an underscore in front of an import statement mean?

  • 2014-01-19 17:25:59
  • Adrian
  • 29709 View
  • 159 Score
  • 3 Answer
  • Tags:   go import

32 Answered Questions

[SOLVED] What does it mean to "program to an interface"?

5 Answered Questions

[SOLVED] What does the star operator mean?

18 Answered Questions

[SOLVED] What does "static" mean in C?

  • 2009-02-21 06:47:52
  • David
  • 799888 View
  • 970 Score
  • 18 Answer
  • Tags:   c syntax static

15 Answered Questions

[SOLVED] What does map(&:name) mean in Ruby?

34 Answered Questions

[SOLVED] What is the difference between an interface and abstract class?

1 Answered Questions

Can a Scanner have a slice receiver?

  • 2018-08-02 08:50:30
  • Rick-777
  • 45 View
  • 0 Score
  • 1 Answer
  • Tags:   go

2 Answered Questions

Is Go able to unmarshal to map[string][]interface{}?

Sponsored Content