blue panther
blue panther

Reputation: 253

Golang Parameters conversion

Can somebody explain how can this happened?

I put interface as parameter in a function. While invoking this function I pass struct into it, but it didn't give me error. Here's the code

package main

import (
    "fmt"
    "github.com/myusername/gomodel/domain"
    "github.com/myusername/gomodel/model"
)

func main() {
    db := model.InitDB()

    newFunc(db)
}


func newFunc(db domain.IUser) {

    r, err := db.CreateUserTable()
    if err != nil {
        fmt.Println("error", err)
    }
    fmt.Println(r)
}

I've implemented the interface somewhere else in the code, because the program just work as the implemented interface expected to be. IUser is an interface whose member is:

type IUser interface {
    CreateUserTable() (sql.Result, error)
}

InitDB is a function to open the database and return struct of database:

type DB struct {
    *sql.DB
}

//InitDB initializes the database
func InitDB() *DB {
    db, err := sql.Open(dbDriver, dbName)
    if err != nil {
        log.Fatal("failed to initialize database: ",err)
    }
    err2 := db.Ping()
    if err2 != nil {
        log.Fatal(err2)
    }

    return &DB{db}
}

My question is: how can a function with a parameter type interface be passed a different type of parameter? And how is this working under the hood?

Upvotes: 1

Views: 310

Answers (1)

Himanshu
Himanshu

Reputation: 12685

As per Golang Spec

An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface.

This is because interface can be implemented as a wrapper to every type. Interface actually points to two things mainly one is the underlying type which is a struct here and other one is the value of that type which is a pointer to DB

You see newFunc is actually taking interface{} as an argument, So you can pass anything to it of type T which can be of primitive types too.

func main() {
    db := model.InitDB()

    newFunc(db)
}

So In case you want to get the underlying value you need to type assert. Interface works like a wrapper to the struct here and save its type and value which can be get using type assertion.

Upvotes: 2

Related Questions