zoukankan      html  css  js  c++  java
  • golang学习笔记——基础知识(1)

    观看B站李文周老师的视频学习golang整理的笔记

     
    变量
    • 定义
    var 变量名 变量类型
    • 多个
    var(
        a  int
        b  int
    )
    • 自动识别变量类型运算符“:=”
    a := 10
    • 匿名变量符“_”
    a,_,c := 1,2,3 //其中2将不会被赋值
    • 定义类型别名
    type 别名 类型  //type cjp int32  需要再main函数外面为类型定义别名
     
    常亮
    • 定义
    const 常量名 = 值
    • 定义多个常亮
    const{
        a = 10
        b = 20
    }
    • iota的使用
    const{
        a = iota  // a = 0
        b = iota  // b = 1
    }
    //当 iota 遇到const时 被重置为0
    const a = iota //a = 0
    const b = iota //b = 0
     
    字符串
    • 获取字符串长度
    str := "hello world"
    fmt.printf("str length is %d" , len(str))  //str length is 11
    • 访问单个字符
    str := "hello world"
    fmt.printf("str[0] is %c",str[0]) //str[0] is h
     
    从键盘获取值
    fmt.Scan(&a)
     
    条件语句
    • switch语句【 switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码,fallthrough不会判断下一条case的expr结果是否为true
    var a int = 2
    switch {
    case a == 2:
             fmt.Print("aaaaaa")
             fallthrough
    case a < 1:
             fmt.Print("bbbbbb")
             fallthrough
    case a == 3:
             fmt.Print("cccccc")
    }
    //输出 aaaaaabbbbbbcccccc
    • for语句
    for i := 10; i < 20; i++ {
        fmt.Print("asdasa")
    }
     
    //使用range关键字
    str := "hello world"
    for k, v := range str {
             fmt.Printf("str[%d] is %c ", k, v)
    }
    • if语句
    if a := 10;a < 20{
        fmt.Println("a的值小于20")
    }
     
     
     
     
     
     
     
  • 相关阅读:
    C#如何取硬件标志
    在C#中利用Excel做高级报表
    C#实现网段扫描
    用C#编写一个抓网页的应用程序
    C# 中操作API
    C#串口操作
    C#里的InputBox
    使用C#进行Word 2002和Excel 2002编程
    用C#快速往Excel写数据
    postgres 查看数据库大小
  • 原文地址:https://www.cnblogs.com/itsuibi/p/14450518.html
Copyright © 2011-2022 走看看