zoukankan      html  css  js  c++  java
  • golang笔记

    Go语言

    1. GO语言的优点

      1. 语法简单

        import "fmt"
        
        var a, b = 1, 2
        a, b = b, a
        fmt.Println(a,b)
        
      2. 静态数据类型、编译语言

        a:=1
        b:=false
        
      3. 内置支持并发

      4. 可以直接编译成机器码

    2. 下载与安装(注意不要安装在有中文的目录)

      1. 下载地址

        官方网站: https://golang.google.cn/

        中文网站: https://studygolang.com/

      2. 下载msi文件,目前最新版本为1.14.1,双击开始安装,这里我安装的位置是Dgolang目录下;

      3. 配置环境变量:

        1. 打开计算机高级系统设置,找到环境变量,在系统环境变量中新建变量名GOROOT,值为刚才安装的目录D:golang
        2. 然后点击系统环境变量中的Path,再点编辑,再点新建,里面输入:%GOROOT%in
        3. 接着点确定、确定;
        4. 打开cmd,输入go version,如果正确显示了版本号,就表示环境变量配置成功,也可以用go env
      4. 环境变量参数解释:

        1. GOROOT:表示go语言库的根目录完整路径;
        2. PATH:path中配置内容方便在命令行快速调用go语言库中的工具;
        3. GOPATH:指定了你的工作空间位置。 注意,它绝对不能和你的Go安装目录相同。
    3. 第一个go语言程序---hello world

      Dgo目录下新建一个文件,文件名称为hello.go,使用文本编辑器notepad++或者vscode打开,在里面输入下面的内容:(其中//后面的内容是注释内容,不影响程序的运行)

      package main  // 包名
      
      import "fmt"  // 引入go语言库中写好的包
      
      func main(){  // 定义一个函数
          fmt.Println("hello world")  // 调用包
      }
      

      输入完成之后,在命令行找到hello.go文件所在的位置,然后输入go run hello.go,如果能够显示hello world,就表示正确运行了;

    4. 注释

      单行注释:

      // 这里是单行注释
      

      多行注释:

      /*
      这里是多行注释
      多行注释1
      多行注释2
      */
      
    5. package关键字

      1. package表示当前代码所属的包(可以理解为一种组织结构,比如你属于哪家公司),其他package通过包名调用这个包下的内容;
      2. package是必须有的,每个文件的package必须存在有效代码的第一行(除注释外);
      3. package main是程序入口包,这个包中可以编写主函数;
    6. import 关键字

      1. import表示引入包,引用其他包的内容;

      2. import "fmt" 表示引用fmt包;fmt包是go语言库中自带的包,实现了输入输出等功能;

      3. import必须存在于package关键字下面,函数或变量声明的上面;

      4. import导入包时,包名两侧必须使用双引号,支持一下集中语法:

        // 一个一个包导入
        import "fmt"
        import "os"
        
        // 一次导入多个包(官方推荐)
        import (
        	"fmt"
            "os"
        )
        
      5. Go语言要求,导入的包都必须使用,否则编译出错,比如如果导入了os包,但是没有使用,会报如下错误信息:

        imported and not used: "os"
        
    7. main函数

      1. func main 称为主函数,是整个程序的入口,最先执行主函数中的代码;

      2. main()后面的{必须和func在同一行,否则运行时提示如下信息:

        .main.go:16:6:syntax error: unexpected semicolon or newline before {
        
      3. 一行语句结束,可以不用写分号;,但是写了也能正常运行,注:C/C++、Java如不写;编译不通过;


    一、解压版GO语言安装包中自带工具

    1. %GOROOT%in下包含的3个.exe

      1. go.exe:编译、运行、构建等都会用到的主程序;
      2. godoc.exe:查看包或者函数的源码;
      3. gofmt.exe:格式化文件;
    2. go.exe可选参数

      Go is a tool for managing Go source code.
      
      Usage:
      
              go <command> [arguments]
      
      The commands are:
      
              bug         start a bug report
              build       compile packages and dependencies
              clean       remove object files and cached files
              doc         show documentation for package or symbol
              env         print Go environment information
              fix         update packages to use new APIs
              fmt         gofmt (reformat) package sources
              generate    generate Go files by processing source
              get         download and install packages and dependencies
              install     compile and install packages and dependencies
              list        list packages or modules
              mod         module maintenance
              run         compile and run Go program
              test        test packages
              tool        run specified go tool
              version     print Go version
              vet         report likely mistakes in packages
      
      Use "go help <command>" for more information about a command.
      
      Additional help topics:
      
              buildmode   build modes
              c           calling between Go and C
              cache       build and test caching
              environment environment variables
              filetype    file types
              go.mod      the go.mod file
              gopath      GOPATH environment variable
              gopath-get  legacy GOPATH go get
              goproxy     module proxy protocol
              importpath  import path syntax
              modules     modules, module versions, and more
              module-get  module-aware go get
              module-auth module authentication using go.sum
              module-private module configuration for non-public modules
              packages    package lists and patterns
              testflag    testing flags
              testfunc    testing functions
      
      Use "go help <topic>" for more information about that topic.
      
    3. 常用参数

      1. go version,查看go语言版本;
      2. go env,查看go语言详细环境;
      3. go list,查看go语言文件目录;
      4. go build,把源码文件构建成系统可执行的文件;
      5. go clean,清空生成的可执行文件;
      6. go vet,静态解析文件,检查是否有语法错误等;
      7. go get,从远程下载第三方go语言库,默认会放在GOPATH目录下;
      8. go bug,提交bug;
      9. go test,测试;
      10. go run,运行文件;

    二、打印输出

    1. Fprint(),在Go web中使用比较多,把内容写入到响应流中;

      源码如下:

      // Fprint formats using the default formats for its operands and writes to w.
      // Spaces are added between operands when neither is a string.
      // It returns the number of bytes written and any write error encountered.
      func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
      	p := newPrinter()
      	p.doPrint(a)
      	n, err = w.Write(p.buf)
      	p.free()
      	return
      }
      

      函数参数中的第一个参数是输出流,后面参数是内容,表示把内容写入到输出流中;第一个结果返回值表示输出内容的长度(字节数),第二个返回值表示错误,如果没有错误取值nil。Fprintln()输出后会添加换行符,所以长度比内多1个;

      Fprint支持三种方式:

      1. Fprint(os.Stdout,”内容1”), 向流中写入内容;
      2. Fprintln(os.Stdout, “内容2”),向流中写入内容之后额外写入换行符;
      3. Fprintf(os.Stdout, “%s”, “内容3”),根据verb格式向流中写入内容;
    2. Print(),将内容写入到控制台中;

      1. Print(“内容1”, “内容2”),输出的内容不换行;
      2. Println(“内容1”, “内容2”),输出的内容自动换行;
      3. Printf(“verb”, “内容”),根据verb输出指定格式内容;
    3. Sprint(),将内容转成string类型返回,需要变量接收;

    三、转义字符

    1. 在go语言中,转义字符又称verb%,能与其他字符组合以表示特殊含义;转义字符在输入和输出语句中使用比较频繁:

      fmt.Printf("verb", "内容")  // 输出
      fmt.Scanf("verb", "接收变量")  // 输入
      

      示例:

      /*
      %d 十进制数
      %x 小写字母十六进制数
      %X 大写字母十六进制数
      %o 八进制数
      %b 二进制数
      %f %g %e 浮点数
      %t 布尔值
      %c 字符
      %s 字符串
      %q 带双引号字符串
      %v 内置格式内容
      %T 类型
      %p 内存地址
      %% 字符%
      
       换行
      	 制表符
      */
      
      fmt.Printf("%d", 19)  // 19
      fmt.Printf("%x  %X", 12, 13)  // c  D
      fmt.Printf("%c", 65)  // C
      fmt.Printf("%c", 97)  // c
      fmt.Printf("%t", false)  // false
      fmt.Printf("%f", 1.6)  // 1.6
      fmt.Printf("%s  %q", "hello", "hello")  // hello  "hello"
      
      // 获取内存地址
      v := 5
      fmt.Printf("%p", &v)  // 0xc00000a100
      
      fmt.Printf("
       增长率为%d%%", 20)  // 增长率为20%
      
  • 相关阅读:
    MVC概念性的内容
    类 class
    php获取真实IP地址
    面向对象static静态的属性和方法的调用
    smarty 入门2(个人总结)
    smarty入门
    读取文件内容fopen,fgets,fclose
    mysql常用命令
    mybatis查询的三种方式
    MyBatis 映射文件
  • 原文地址:https://www.cnblogs.com/ixuer/p/12544270.html
Copyright © 2011-2022 走看看