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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。