Golang Invalid Receiver Type in Method Func

This is the best I can come up with:

type Connection ssh.Client

func (conn *Connection) SendCommand() ([]byte, error) {
    (*ssh.Client)(conn).NewSession()

Note that I've changed the type to not be a pointer type (but then I've made a pointer receiver for SendCommand). I'm not sure there's any way to create a function with a pointer type as a receiver.


Another option is to use type aliasing to achieve the desired behavior. I was trying to do something "clever" for readability:

type foo struct {
    i int
}

type foo_ptr = *foo

type foo_ptr_slice = []foo_ptr
type foo_ptr_map = map[string]foo_ptr
type foo_ptr_slice_map = map[string]foo_ptr_slice

func (r foo_ptr) dump() {
    fmt.Printf("%d\n", r.i)
}

func main() {
    // need a map of slice of pointers
    var m foo_ptr_map

    m = make(foo_ptr_map, 0)

    m["test"] = &foo{i: 1}

    var m2 foo_ptr_slice_map

    m2 = make(foo_ptr_slice_map, 0)
    m2["test"] = make(foo_ptr_slice, 0, 10)

    m2["test"] = append(m2["test"], &foo{i: 2})

    fmt.Printf("%d\n", m["test"].i)
    fmt.Printf("%d\n", m2["test"][0].i)
    m["test"].dump()
}

I acknowledge that type aliasing is used for large-scale refactoring but this seems like a very good use for readability sake.


You can't declare a new type with a pointer TypeSpec. Also declaring a new type is used specifically to remove the entire method set, so you won't have any of the original methods from the *ssh.Client.

What you want is to use composition by embedding the *ssh.Client in your own struct type:

type Connection struct {
    *ssh.Client
}

func Connect(addr, user, password string) (*Connection, error) {
    sshConfig := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
        HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
    }

    conn, err = ssh.Dial("tcp", addr, sshConfig)
    if err != nil {
        return nil, err
    }

    return &Connection{conn}, nil
}

func (conn *Connection) SendCommand() ([]byte, error) {
    session, err := conn.NewSession()
    // ...
}