如何使用gofmt将单引号替换为双引号?

huangapple go评论69阅读模式
英文:

How to use gofmt to replace single quote with double quote

问题

我使用go 1.6,并且喜欢使用单引号。在编辑完文件后,我想在终端中使用gofmt来替换它们,但是没有成功。

gofmt -r "'->"'" book.go
解析模式时出错:在1:1处未终止字符文字

我使用zsh。

英文:

I use go 1.6 and I love using single quotes. After I finish editing my file, in my terminal I'd like to use gofmt for replacing them, but nothing works.

    gofmt -r "'->\"" book.go
    parsing pattern ' at 1:1: rune literal not terminated

I use zsh.

答案1

得分: 4

$ go doc cmd/gofmt

Gofmt 格式化 Go 程序。

标志如下:

-r rule
    在重新格式化之前将重写规则应用于源代码。

使用 -r 标志指定的重写规则必须是以下形式的字符串:

pattern -> replacement

模式和替换都必须是有效的 Go 表达式。在模式中,单字符小写标识符用作通配符,匹配任意子表达式;这些表达式将被替换中的相同标识符替换。


Go 编程语言规范

表达式

表达式通过将运算符和函数应用于操作数来指定值的计算。


模式和替换都必须是有效的 Go 表达式。

英文:

> $ go doc cmd/gofmt
>
> Gofmt formats Go programs.
>
> The flags are:
>
> -r rule
> Apply the rewrite rule to the source before reformatting.
>
> The rewrite rule specified with the -r flag must be a string of the
> form:
>
> pattern -> replacement
>
> Both pattern and replacement must be valid Go expressions. In the
> pattern, single-character lowercase identifiers serve as wildcards
> matching arbitrary sub-expressions; those expressions will be
> substituted for the same identifiers in the replacement.


> The Go Programming Language Specification
>
> Expressions
>
> An expression specifies the computation of a value by applying
> operators and functions to operands.


Both pattern and replacement must be valid Go expressions.

huangapple
  • 本文由 发表于 2016年3月4日 04:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/35782213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定