doubu7134 2019-06-17 21:27
浏览 42
已采纳

Golang中的struct概念需要协助

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

  • 写回答

1条回答 默认 最新

  • dtrj21373 2019-06-17 22:55
    关注

    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")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题