The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
fumeboy

询问下 Go 的这个语法是否存在

  •  
  •   fumeboy · Jul 12, 2022 · 2841 views
    This topic created in 1402 days ago, the information mentioned may be changed or developed.

    原文地址是 https://go.dev/ref/spec#PrimaryExpr

    原文内容是

    PrimaryExpr =
    	Operand |
    	Conversion |
    	MethodExpr |
    	PrimaryExpr Selector |
    	PrimaryExpr Index |
    	PrimaryExpr Slice |
    	PrimaryExpr TypeAssertion |
    	PrimaryExpr Arguments .
    
    Selector       = "." identifier .
    Index          = "[" Expression "]" .
    Slice          = "[" [ Expression ] ":" [ Expression ] "]" |
                     "[" [ Expression ] ":" Expression ":" Expression "]" .
    TypeAssertion  = "." "(" Type ")" .
    Arguments      = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
    

    其中我关心的是 Arguments 的定义,它表示为 ( ExpressionList | Type [ "," ExpressionList ] )

    ExpressionList 很容易理解,它可以匹配 (1,2,string(1)) 这样的文本,但是 Type [ExpressionList] 怎么理解呢,我从来没见过在 go 里可以写 var v = func_call(TypeName) 这样的表达式

    (在上面引用的文本中,[ ] 包裹的内容表示为可选, | 表示匹配其中一个)

    5 replies    2022-07-12 19:58:09 +08:00
    blless
        1
    blless  
       Jul 12, 2022
    定义接口的时候可以吧参数名省略 直接写类型
    blless
        2
    blless  
       Jul 12, 2022
    额 感觉是我理解错了 确实没见过。。不知道参数定义算不算参数语法表达式
    mainjzb
        3
    mainjzb  
       Jul 12, 2022   ❤️ 1
    make
    new
    EAFA0
        4
    EAFA0  
       Jul 12, 2022
    var action = func(context.Context)

    是这样的吗? 这个语法是可行的
    chai2010
        5
    chai2010  
       Jul 12, 2022
    ast 解析时,type 和表达式是不怎么区分的,new(int)里 int 可以是类型也可能被重新定义为一个变量。真正的语义需要在 Go/types 包处理。

    欢迎关注《 Go 语言定制指南》 https://github.com/chai2010/go-ast-book
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1042 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 22:35 · PVG 06:35 · LAX 15:35 · JFK 18:35
    ♥ Do have faith in what you're doing.