Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend package struct in golang

Tags:

struct

go

is it possible in golang extend struct (something like extend a class in other languages, and use it with functions for old one)

I have https://github.com/xanzy/go-gitlab/blob/master/services.go#L287 type SetSlackServiceOptions

package gitlab

// SetSlackServiceOptions struct
type SetSlackServiceOptions struct {
    WebHook  *string `url:"webhook,omitempty" json:"webhook,omitempty" `
    Username *string `url:"username,omitempty" json:"username,omitempty" `
    Channel  *string `url:"channel,omitempty" json:"channel,omitempty"`
}

And I want to add some fields to the type in my own package. Is It possible to do it, that I can call function SetSlackService with this my own new type?

package gitlab

func (s *ServicesService) SetSlackService(pid interface{}, opt *SetSlackServiceOptions, options ...OptionFunc) (*Response, error) {
    project, err := parseID(pid)
    if err != nil {
        return nil, err
    }
    u := fmt.Sprintf("projects/%s/services/slack", url.QueryEscape(project))

    req, err := s.client.NewRequest("PUT", u, opt, options)
    if err != nil {
        return nil, err
    }

    return s.client.Do(req, nil)
}

https://github.com/xanzy/go-gitlab/blob/266c87ba209d842f6c190920a55db959e5b13971/services.go#L297

Edit:

I want to pass own structure into function above. It's function from gitlab package and I want to extend the http request

like image 235
JAttanonRadar Avatar asked Jan 15 '18 10:01

JAttanonRadar


People also ask

Is there inheritance in go?

Go doesn't have inheritance – instead composition, embedding and interfaces support code reuse and polymorphism.


1 Answers

In go, structures cannot be extended like classes as in other object oriented programming languages. Nor we can add any new field in an existing struct. We can create a new struct embedding the struct that we require to use from different package.

package gitlab

// SetSlackServiceOptions struct
type SetSlackServiceOptions struct {
    WebHook  *string `url:"webhook,omitempty" json:"webhook,omitempty" `
    Username *string `url:"username,omitempty" json:"username,omitempty" `
    Channel  *string `url:"channel,omitempty" json:"channel,omitempty"`
}

In our main package we have to import gitlab package and embed our struct as below:

package main

import "github.com/user/gitlab"

// extend SetSlackServiceOptions struct in another struct
type ExtendedStruct struct {
  gitlab.SetSlackServiceOptions
  MoreValues []string
}

Golang spec define embedded types in struct as:

A field declared with a type but no explicit field name is called an embedded field. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

// A struct with four embedded fields of types T1, *T2, P.T3 and *P.T4
struct {
    T1        // field name is T1
    *T2       // field name is T2
    P.T3      // field name is T3
    *P.T4     // field name is T4
    x, y int  // field names are x and y
}
like image 187
Himanshu Avatar answered Oct 16 '22 21:10

Himanshu