go入门demo

go快速学习:

官网学习方案(选择自己喜欢的语言):A Tour of Go

视频:B站是个不错的选择
 (转go入门使用)推荐视频:
1-课程需知与课程提纲_哔哩哔哩_bilibili1-课程需知与课程提纲是8小时转职Golang工程师(如果你想低成本学习Go语言)的第1集视频,该合集共计52集,视频收藏或关注UP主,及时了解更多相关视频内容。https://www.bilibili.com/video/BV1gf4y1r79E/?p=1&spm_id_from=pageDriver&vd_source=7f4825d3e8c655d4767b206d86d944db

https://www.bilibili.com/video/BV1XY4y1t76G?p=1&spm_id_from=pageDriver&vd_source=7f4825d3e8c655d4767b206d86d944db

推荐go编辑idea:推荐vscode (Visual Studio Code - Code Editing. Redefined)           golang(付费,头一个月免费,破解另说)

go环境安装:Download and install - The Go Programming Language

idea插件:vscode      (goland也是一个不错的选择 JetBrains GoLand:不只是 Go IDE)

当ctrl shift  l  格式化不起作用时,可能是插件未安装,尝试一下下面的方式

入门安装遇到的问题 1.vscode推荐的安装install (重启打开)   2.因为插件原因破坏,可能需要卸载后重新安装

vscode命令方式安装必要的插件: 控制台: go  vet .    

 历史安装描述

D:\go\crm\dbutils>Finished running tool: C:\Go\bin\go.exe build -o C:\Users\haoha\AppData\Local\Temp\vscode-goan5lqB\go-code-check .

D:\go\crm\dbutils>Finished running tool: C:\Go\bin\go.exe vet .

Installing github.com/cweill/gotests/[email protected] (C:\Users\haoha\go\bin\gotests.exe) SUCCEEDED
Installing github.com/cweill/gotests/[email protected] (C:\Users\haoha\go\bin\gotests.exe) SUCCEEDED
Installing github.com/fatih/[email protected] (C:\Users\haoha\go\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/fatih/[email protected] (C:\Users\haoha\go\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/josharian/[email protected] (C:\Users\haoha\go\bin\impl.exe) SUCCEEDED
Installing github.com/josharian/[email protected] (C:\Users\haoha\go\bin\impl.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/[email protected] (C:\Users\haoha\go\bin\goplay.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/[email protected] (C:\Users\haoha\go\bin\goplay.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (C:\Users\haoha\go\bin\dlv.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (C:\Users\haoha\go\bin\dlv.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (C:\Users\haoha\go\bin\staticcheck.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (C:\Users\haoha\go\bin\staticcheck.exe) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (C:\Users\haoha\go\bin\gopls.exe) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (C:\Users\haoha\go\bin\gopls.exe) SUCCEEDED

All tools successfully installed. You are ready to Go. :)
Installing github.com/ramya-rao-a/[email protected] (C:\Users\haoha\go\bin\go-outline.exe) SUCCEEDED

All tools successfully installed. You are ready to Go. :)

推荐其他插件:Tabnine AI      tab补全代码

go安装后环境查看及其他常用命令

go mod init  模块名

命令:go mod init im

go mod tidy 

编写main.go后就可以使用 go  run main.go  或者 go  run .

之后了解 package 、  数据类型(基本数据类型 ,复杂数据类型 ) 、函数  、import、  指针    struct (类似class概念)、channel 、   comparable  、值传递和引用传递(推荐官网方式)

if   for循环   switch    select  等

基本数据类型

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

命令集合

go mod download    下载依赖的module到本地cache(默认为$GOPATH/pkg/mod目录)
go mod edit        编辑go.mod文件
go mod graph       打印模块依赖图
go mod init  模块名        初始化当前文件夹, 创建go.mod文件
go mod tidy        增加缺少的module,删除无用的module
go mod vendor      将依赖复制到vendor下
go mod verify      校验依赖
go mod why         解释为什么需要依赖

入门程序案例

package main

import (
	"fmt"
	"im/lib1"   // . "im/lib1" 点号导包不推荐
	_ "im/lib1" //匿名导包
	mylib "im/lib2"
)

// 启动加载项  const  var   init  main func
func main() {
	r1, r2 := f(2, 3)
	fmt.Println(r1, r2)

	lib1.TestLib()
	a := 18
	fmt.Println()
	fmt.Println("修改前的值:", a, &a)
	changeValue(&a)
	fmt.Println("修改后的值:", a)

	pa := 10
	pb := 20
	swap(&pa, &pb)
	fmt.Println(pa, pb)
	mylib.Test()

	fmt.Println("=======sumx==========")
	lib1.GetSum()
	fmt.Println("=======sumx==========")
}

func f0(a int, b int) (int, int) {
	r1 := 10
	r2 := 10
	return r1, r2
}

func f(a int, b int) (r1, r2 int) {
	r1 = 10
	r2 = 10
	return
}

// 指针即地址   取指针类型中的值用*,取地址第一种为&取地址,第二种指针本身为地址值
func changeValue(a *int) {
	*a = 10
	fmt.Println(a) //本身输出为地址值
}

func swap(a *int, b *int) {
	var tmp int = *a
	*a = *b
	*b = tmp
}

vscode  debug调试配置文件

{

    // Use IntelliSense to learn about possible attributes.

    // Hover to view descriptions of existing attributes.

    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Launch Package",

            "type": "go",

            "request": "launch",

            "mode": "auto",

            "program": "${workspaceFolder}\\admin-service\\main.go",

            "args": [

                "server"

            ]

        }

    ]

}

猜你喜欢

转载自blog.csdn.net/haohaounique/article/details/131620410
今日推荐