我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

golang是否等效于此Python短路习惯用法“ z = x或y”

In Python z = x or y can be understood as assign z as if x is falsey, then y, else x, is there a similar idiom in golang?

Specifically the two variables on the right are strings, I'd like to assign the first if it's non-emtpy, otherwise the second.

No, you have to use if/else:

s1, s2 := "", "hello"
var x string

if s1 == "" {
    x = s2
} else {
    x = s1
}

近期文章

  • Golang的Go Routine背后的架构是什么?
  • 使用两种不同(但相同)的类型时,类型声明失败
  • 管理Golang中的错误
  • 形成结构以使用golang中的复合键读取bson对象
  • 不了解Go中的组成
  • Golang Google警报XML解析
  • 使用aws-sdk-go将文件保存到S3
  • Golang:为什么compress / gzip Read函数不能读取文件内容?
  • Golang正则表达式报价子匹配
  • Golang:遍历切片并生成HTML表
  • 创建XML元素而不关闭标签
  • 缓冲的通道工作人员恐慌
  • 将[] uint8 / [] byte转换为哈希表GoLang
  • 无法使用Go连接到MS SQL Server
  • 在另一个函数中使用SQL连接
  • Golang将参数传递给大猩猩路由器
  • 如何在golang或一般情况下找到进程正在使用的端口?
  • 每个唯一ID最多运行一个并发线程的算法
  • 带有光标的Google App Engine数据存储区查询不会迭代所有项
  • c.Infof未定义(类型context.Context没有字段或方法Infof)google.golang.org/appengine/log错误

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华