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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?