donglun2024 2018-12-07 11:26
浏览 115
已采纳

Golang使用bufio.Scanner多次扫描同一行

I am writing a code which scans a test file and replaces the text with something else. Would like to replace same set of lines with different texts stacked one below the other. One option i found is using the tee function of ioreader, but is there a better way to achieve the same.

For instance, what i am trying to achieve is replacing the methodtype substring in below piece of text with different strings like GET, POST, etc. To get the output as below:

TEXT:

router.Methods("methodtype").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
httptransport.EncodeResponse,
append(options, kitHttp.ServerBefore())...

OUTPUT:

router.Methods("GET").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
httptransport.EncodeResponse,
append(options, kitHttp.ServerBefore())...


router.Methods("PUT").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
httptransport.EncodeResponse,
append(options, kitHttp.ServerBefore())...

router.Methods("POST").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
httptransport.EncodeResponse,
append(options, kitHttp.ServerBefore())...
  • 写回答

1条回答 默认 最新

  • doupai8533 2018-12-07 16:57
    关注

    In order to replace text, you can use text/template instead of bufio:

    Playground: https://play.golang.org/p/7HYBqxtJ3KB

    package main
    
    import (
        "fmt"
        "text/template"
        "os"
    )
    
    type method struct {
        MethodType string
    }
    
    func main() {
    
        buf := `router.Methods("{{.MethodType}}").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
    httptransport.EncodeResponse, append(options, kitHttp.ServerBefore())...
    `
    
        tmpl, err := template.New("test").Parse(buf)
        if err != nil {
            panic(err)
        }
    
        methods := []string{"GET", "PUT", "POST"}
    
        for _, m := range methods {
            err = tmpl.Execute(os.Stdout, method{MethodType: m})
            if err != nil {
                fmt.Println(err.Error())
            }
        }
    
    }
    

    Output:

    router.Methods("GET").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
    httptransport.EncodeResponse, append(options, kitHttp.ServerBefore())...
    router.Methods("PUT").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
    httptransport.EncodeResponse, append(options, kitHttp.ServerBefore())...
    router.Methods("POST").Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint
    httptransport.EncodeResponse, append(options, kitHttp.ServerBefore())...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大