处理变量的范围:内部循环

2024-04-29

作为一名直接进入 Go 的 JS 开发者,如果长度超过commits不止一个。我没有太多时间来完成这件事,而且我搜索的时间比我希望的要长。关于如何重组它或让它发挥作用有什么想法吗?

        case github.PushPayload:
            push := payload.(github.PushPayload)
            repo := push.Repository.Name
            owner := push.Repository.Owner.Login
            ownerUrl := push.Repository.Owner.HTMLURL
            ownerAvatar := push.Repository.Owner.AvatarURL
            ref := push.Ref
            refTrim := strings.Replace(ref, "/.*/", "", -1)
            commits := push.Commits


            for _, commit := range commits {
                message := commit.Message
                url := commit.URL
                id := commit.ID
                idShort := id[0:6]
                committer := commit.Committer.Username
                description := "[`" + idShort + "`](" + url + ") " + message + " - " + committer
            }


            if len(commits) == 1 {
                discord.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
                    Color:       0x00B1FF,
                    Description: description,
                    Title:       "[" + repo + ":" + refTrim + "] 1 new commit! ????",
                    Author: &discordgo.MessageEmbedAuthor{
                        Name:    owner,
                        URL:     ownerUrl,
                        IconURL: ownerAvatar,
                    },
                })
            } else {
                discord.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
                    Color:       0x00B1FF,
                    Description: description,
                    Title:       "[" + repo + ":" + refTrim + "] " + string(len(commits)) + " new commits! ⚒️",
                    Author: &discordgo.MessageEmbedAuthor{
                        Name:    owner,
                        URL:     ownerUrl,
                        IconURL: ownerAvatar,
                    },
                })
            }

PS:忽略string(len(commits))我知道这是不正确的,我首先要解决这个问题。也忽略:refTrim := strings.Replace(ref, "/.*/", "", -1),这也不正确。


您必须在循环之前声明描述变量,然后在循环内向其添加提交描述,如下所示。

注意:我在这里发出了其他代码块。

    description := ""
    for _, commit := range commits {
        description = description + "commit description here"
    }

description += "commit description here" 也可以在循环内部使用。但我在上面添加了更多理解。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

处理变量的范围:内部循环 的相关文章