亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Go 中的 README.md 中的注釋標簽之間插入鏈接

在 Go 中的 README.md 中的注釋標簽之間插入鏈接

Go
千萬里不及你 2022-06-01 09:52:51
我想在我的README.md文件中的評論標簽之間插入鏈接,因為我正在動態生成鏈接。我寫了一個函數來做到這一點,但問題是它也替換了評論標簽。我需要修改我的函數以插入評論標簽之間的鏈接,而不是整體替換它。//README.md### HTTP APIs<!--HTTP-API-start--><!--HTTP-API-end-->### AMQP APIs<!--AMQP-API-start--><!--AMQP-API-end-->這是我為插入鏈接而編寫的函數。一種可能的解決方案是將注釋標簽與字符串一起附加httpAPI,AmqpAPI但這不是我想要的,因為它替換了文件中的當前標簽。func GenerateGodocLinkInReadme(amqpLinks string, httpLinks string) {    path := `../../README.md`    formattedContent, err := ioutil.ReadFile(path)    if err != nil {        panic(err)    }    httpAPI := "<!--HTTP-API-start-->" +        amqpLinks +        "\n" +        "<!--HTTP-API-end-->"    AmqpAPI := "<!--AMQP-API-start-->" +        httpLinks +        "\n" +        "<!--AMQP-API-end-->"    formattedContent = regexp.MustCompile(`<!--AMQP-API-start-->([\s\S]*?)<!--AMQP-API-end-->`).ReplaceAll(formattedContent, []byte(AmqpAPI))    exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))    formattedContent = regexp.MustCompile(`<!--HTTP-API-start-->([\s\S]*?)<!--HTTP-API-end-->`).ReplaceAll(formattedContent, []byte(httpAPI))    exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))}此功能正常工作,但它也替換了評論標簽。我需要修改這個函數,以便它在評論標簽之間插入鏈接。
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

嘗試這個。


func GenerateGodocLinkInReadme(amqpLinks string, httpLinks string) {

    path := `README.md`

    formattedContent, err := ioutil.ReadFile(path)

    if err != nil {

        panic(err)

    }

    amqpRegex := regexp.MustCompile(`<!--AMQP-API-start-->([\s\S]*?)<!--AMQP-API-end-->`)

    httpRegex := regexp.MustCompile(`<!--HTTP-API-start-->([\s\S]*?)<!--HTTP-API-end-->`)


    prevAmqpLinks := string(amqpRegex.FindSubmatch((formattedContent))[1]) // Second index of returns links between tags

    prevHttpLinks := string(httpRegex.FindSubmatch((formattedContent))[1]) // Second index of returns links between tags

    httpAPI := prevHttpLinks + httpLinks + "\n"

    AmqpAPI := prevAmqpLinks + amqpLinks + "\n"

    formattedContent = amqpRegex.ReplaceAll(formattedContent, []byte(`<!--AMQP-API-start-->` + AmqpAPI + `<!--AMQP-API-end-->`))

    formattedContent = httpRegex.ReplaceAll(formattedContent, []byte(`<!--HTTP-API-start-->` + httpAPI + `<!--HTTP-API-end-->`))

    exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))

}


查看完整回答
反對 回復 2022-06-01
  • 1 回答
  • 0 關注
  • 286 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號