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

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())...

图片转代码服务由CSDN问答提供 功能建议

我正在编写一个代码,该代码可以扫描测试文件并将其替换为其他内容。 想要用堆叠在另一个下方的不同文本替换同一行。 我发现的一个选项是使用ioreader的tee函数,但是有更好的方法来实现该功能。

例如,我要实现的是替换 methodtype 文本下方的子字符串,带有不同的字符串,例如 GET POST 等。 要获得如下输出:

TEXT:

  router.Methods(“ methodtype”)。Path(templatepackagespec.MethodtypePath).Handler(kitHttp.NewServer(endpoints.FuncnameEndpoint 
httptransport.EncodeResponse,\  nappend(options,kitHttp.ServerBefore())... 
   
 
 

输出:

  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条回答 默认 最新

相关推荐 更多相似问题