-3

I want to use slacknotificationprovider in the NewNotifier function. How can I do it. Also I want send a string(config.Cfg.SlackWebHookURL) in newNotifier function. What should I do? Also please suggest me some material to get a deeper knowledge of struct and interface in golang. I also want to know why ProviderType.Slack is not defined as I have mentioned it in ProviderType struct as of SlackNotificationProvider type? Thanks.

type SlackNotificationProvider struct {
    SlackWebHookURL string
    PostPayload     PostPayload
}
type ProviderType struct {
    Slack   SlackNotificationProvider
    Discord DiscordNotificationProvider
}
type Notifier interface {
    SendNotification() error
}
func NewNotifier(providerType ProviderType) Notifier {
    if providerType == ProviderType.Slack {
        return SlackNotificationProvider{
            SlackWebHookURL: SlackWebHookURL,
        }
    } else if providerType == ProviderType.Discord {
        return DiscordNotificationProvider{
            DiscordWebHookURL: SlackWebHookURL + "/slack",
        }
    }
    return nil
}
slackNotifier := NewNotifier(config.Cfg.SlackWebHookURL)

Errors: 1. cannot use config.Cfg.SlackWebHookURL (type string) as type ProviderType in argument to NewNotifiergo 2. ProviderType.Slack undefined (type ProviderType has no method Slack)go

  • 4
    Have a look at some docs first to understand more about the language. For example: https://gobyexample.com/structs, https://gobyexample.com/interfaces and https://golang.org/doc/effective_go.html – georgeok Jun 17 '19 at 21:34
  • 2
    And of course the [Tour of Go](https://tour.golang.org) – JimB Jun 17 '19 at 21:36
  • I have tried these sites before but can't get much information from it. I got confused with its use. I understand its part with normal data types but not with structs. @georgeok – Shubham Agarawal Jun 17 '19 at 21:45
  • You would be better off getting a book and understand the fundamental of the language. – georgeok Jun 17 '19 at 21:52
  • Can you please help me with this problem as I have to submit my assignment by tomorrow and I have less time left @georgeok. After completing my assignment I will purchase a good book for it and clear my concepts. Thanks – Shubham Agarawal Jun 17 '19 at 21:55
  • I can give you a couple of hints and hope you'll get the job done. SlackNotificationProvider and Notifier are different types. ProviderType.Slack is invalid syntax since Slack filed has to be initialised on the struct instance. Type comparison is done with someInterface.(type). – georgeok Jun 17 '19 at 22:08
  • Thanks, @georgeok. I will try to solve my problems with it. – Shubham Agarawal Jun 17 '19 at 22:17
  • @georgeok https://stackoverflow.com/a/24809384/11661209 In this example, they have initialized it in the same way I had done. What is the difference then? Also, this is my Notifier interface ```golang type Notifier interface { SendNotification() error } ``` – Shubham Agarawal Jun 17 '19 at 22:23
  • @georgeok, please help. I am very much confused. – Shubham Agarawal Jun 17 '19 at 22:39

1 Answers1

0

Golang is a strongly typed language, which means the arguments to your functions are defined and can't be different. Strings are strings and only strings, structs are structs and only structs. Interfaces are golang's way of saying "This can be any struct that has method(s) with the following signatures". So you can't pass a string as a ProviderType and none of your structs actually implement the interface method you've defined, so nothing would work as you've laid it out. To reorganize what you've got into something that might work:

const (
    discordType = "discord"
    slackType = "slack"
)

// This means this will match any struct that defines a method of 
// SendNotification that takes no arguments and returns an error
type Notifier interface {
    SendNotification() error
}

type SlackNotificationProvider struct {
    WebHookURL string
}

// Adding this method means that it now matches the Notifier interface
func (s *SlackNotificationProvider) SendNotification() error {
    // Do the work for slack here
}

type DiscordNotificationProvider struct {
   WebHookURL string
}

// Adding this method means that it now matches the Notifier interface
func (s *DiscordNotificationProvider) SendNotification() error {
    // Do the work for discord here
}

func NewNotifier(uri, typ string) Notifier {
    switch typ {
    case slackType:
       return SlackNotificationProvider{
            WebHookURL: uri,
        }
    case discordType:
        return DiscordNotificationProvider{
            WebHookURL: uri + "/slack",
        }
    }
    return nil
}
// you'll need some way to figure out what type this is
// could be a parser or something, or you could just pass it
uri := config.Cfg.SlackWebHookURL
typ := getTypeOfWebhook(uri)
slackNotifier := NewNotifier(uri, typ)

As far as documentation to help with this, the "Go By Example" stuff is good, and I see other people have already linked that. That said, a struct with one method feels like it should be a function, which you can also define as a type to allow you to pass back a few things. Example:

type Foo func(string) string

func printer(f Foo, s string) {
    fmt.Println(f(s))
}

func fnUpper(s string) string {
    return strings.ToUpper(s)
}

func fnLower(s string) string {
    return strings.ToLower(s)
}

func main() {
   printer(fnUpper, "foo")
   printer(fnLower, "BAR")
}
Tim Brown
  • 3,173
  • 1
  • 18
  • 15