golang - 为什么++和 - 不能在表达式中工作? [英] golang - Why ++ and -- not work in expressions?

查看:177
本文介绍了golang - 为什么++和 - 不能在表达式中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们认为在其他语言中理所当然的想法并且几乎期望它能够顺利运行 - 这种做法几乎如此自然,那么编译器为什么不开心呢?只是觉得有时候不想走开。

增加值的唯一方法是将它放在它自己的单独行中?

http://play.golang.org/p/_UnpZVSN9n

  package main 

importfmt
importstrconv

func main(){
a:= 1
//评估表达式并传入函数 - 不起作用
fmt.Println(strconv.Itoa(a ++))
//围绕++的大括号也不会工作
fmt.Println(strconv.Itoa((a ++)))
}



What we take for granted in other languages and almost expect it to work in go, won't work - its almost so natural to do this, so why isn't the compiler happy? Just feeling like bailing out of go sometimes.

The only way to increment the value is to put it in its own separate line?

http://play.golang.org/p/_UnpZVSN9n

package main

import "fmt"
import "strconv"

func main() {
    a := 1
    //Evaluate expression and pass into function - won't work
    fmt.Println(strconv.Itoa(a++))
    //Braces around a++ also won't work
    fmt.Println(strconv.Itoa((a++)))
}

解决方案

++ and -- are statements in golang, not expressions

这篇关于golang - 为什么++和 - 不能在表达式中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆