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

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


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

查看所有标签

猜你喜欢:

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

Text Algorithms

Text Algorithms

Maxime Crochemore、Wojciech Rytter / Oxford University Press / 1994

This much-needed book on the design of algorithms and data structures for text processing emphasizes both theoretical foundations and practical applications. It is intended to serve both as a textbook......一起来看看 《Text Algorithms》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具