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

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

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

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


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

查看所有标签

猜你喜欢:

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

How to Design Programs, 2nd Edition

How to Design Programs, 2nd Edition

Matthias Felleisen、Robert Bruce Findler、Matthew Flatt、Shriram Krishnamurthi / MIT Press / 2018-5-4 / USD 57.00

A completely revised edition, offering new design recipes for interactive programs and support for images as plain values, testing, event-driven programming, and even distributed programming. This ......一起来看看 《How to Design Programs, 2nd Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器