Golang 学习之“”、nil 和 len(s)的对比

栏目: Go · 发布时间: 7年前

1.”“用法

使用”“判断string变量是否为空。

输入:

var s string 

  if s == "" { 

      fmt.Println("s is empty") 

 } else { 

     fmt.Println("s is not empty") 

}

输出:

s is empty

2.nil用法

使用nil判断结构体的指针是否为空。

输入:

var Str struct { 

    name string 

    age int 

} 

func main() { 

    var s *Str 

    if s == nil { 

    fmt.Println(" s is nil ") 

    } else { 

        fmt.Println(" s is not nil ") 

    } 

} 

输出: s is nil

3.len(s)用法

用于求数组、切片和字典的长度。

输入:

func main () { 

    var s [] string 

    if len(s) == 0 { 

        fmt.Println("s is empty") 

    } else { 

        fmt.Println("s is not empty") 

    } 

} 

输出: s is empty

注:个人总结,如有错误欢迎指出,谢谢!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Development Recipes

Web Development Recipes

Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00

You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具