
golang
kong-kong
记录流水账
展开
-
gin的restful风格例子
新增// addr.POST("/employee", func(c *gin.Context) { //var username = c.PostForm("username") //var name = c.PostForm("name") var username = c.Query("username") var name = c.Query("name") var age,err = strconv.Atoi(c.Query("age")) if err!=nil { age原创 2022-04-10 00:16:12 · 363 阅读 · 0 评论 -
gin接收参数例子
r变量r := gin.Default()Query类似springmvc中的@RequestParam// addr.POST("/employee", func(c *gin.Context) { //var username = c.PostForm("username") //var name = c.PostForm("name") var username = c.Query("username") var name = c.Query("name") var age,e原创 2022-04-10 00:06:39 · 955 阅读 · 1 评论 -
gin安装
环境要求Go 1.13 及以上版本安装要安装 Gin 软件包,需要先安装 Go 并设置 Go 工作区下载并安装 gingo get -u github.com/gin-gonic/gin将 gin 引入到代码中import "github.com/gin-gonic/gin"开始新建server.goserver.go内容package mainimport "github.com/gin-gonic/gin"func main() { r := gin.Default(原创 2022-04-06 21:56:14 · 1228 阅读 · 0 评论 -
golang问题集锦
no required module provides packageno required module provides packagethe selected directory is not a valid home for go sdkthe selected directory is not a valid home for go sdk原创 2022-04-05 22:03:53 · 322 阅读 · 0 评论 -
no required module provides package
no required module provides packageno required module provides package github.com/satori/go.uuid: go.mod file not found in current directory or any parent directory; see ‘go help modules’原因安装过go高版本解决方式go env -w GO111MODULE=auto或者go env -w GO111MO转载 2022-04-05 22:01:42 · 8429 阅读 · 0 评论 -
the selected directory is not a valid home for go sdk
原因从go 1.15升级到go 1.18后,goland2020.3切换sdk,报如下错解决版本查看go版本go versiongo version go1.18 windows/amd64编辑zversion.go文件{GOROOT}/src/runtime/internal/sys/zversion.go添加下面语句const TheVersion = `go1.18`重启goland参考文档参考文档...原创 2022-04-05 20:13:47 · 700 阅读 · 2 评论 -
struct之demo1
Storageos1package mystructtype Storageos1 struct { PodNamespace string PvName string VolName string volNamespace string ReadOnly bool description string pool string fsType string SizeGB int Labels map[原创 2021-08-19 23:25:55 · 334 阅读 · 0 评论 -
go之struct
struct结构体定义type struct_var_type struct { member definition member definition member definition ......}语法# 不推荐这种ariable_name := struct_var_type {value1, value2,......}variable_name := structure_variable_type { key1: value1, key2: value2.原创 2021-08-03 19:38:14 · 231 阅读 · 0 评论 -
解决go包管理代理网址无法访问:proxy.golang.org
报错信息o: github.com/NYTimes/gziphandler@v1.1.1: Get “https://proxy.golang.org/github.com/%21n%21y%21times/gziphandler/@v/v1.1.1.mod”: dial tcp 172.217.160.113:443: connectex: A connection attempt解决goland# 在goland的Terminal设置下面命令go env -w GOPROXY=https:原创 2021-07-30 22:17:52 · 1457 阅读 · 0 评论 -
go函数make、slice、append
make// 长度和容量都是5个元素 array := make([]string,5) for i:=0;i<len(array);i++ { array[i] = strconv.Itoa(i) // int to string fmt.Printf("%d=%s \n",i,array[i]) } fmt.Println() //长度为3个 容量为5个 array1 := make([]string,3,5) for i:=0;i<len(array1);i+原创 2021-01-30 21:45:51 · 672 阅读 · 0 评论 -
go-func
函数定义函数名大写开头,可以被其他包访问函数名小写开头,只能被本包访问func funcname(p1 type1, p2 type2,...) (result1 type1 , result2, type2 ...)单个返回值-例子func add(a ,b int) int { return a+b}调用 c := add(1,8); fmt.Printf("c=%d \n",c)多个返回值func moreOper(a ,b int) (int,int) { re原创 2021-01-25 20:56:47 · 291 阅读 · 0 评论 -
go-goto
goto例子1for i:=0;i<5;i++ { for j:=0;j<5;j++ { if j>3 { goto GOBREAK } fmt.Printf("current i=%d,j=%d \n",i,j) } } GOBREAK: fmt.Printf("goto end ---------------")输出current i=0,j=0current i=0,j=1current i=0,j=2current i=原创 2021-01-25 20:23:59 · 205 阅读 · 0 评论 -
go-continue
continue例子1for i:=0;i<10;i++ { fmt.Printf("start current i=%d \n",i) if i>5 { continue } fmt.Printf("end current i=%d \n",i) }start current i=0end current i=0start current i=1end current i=1start current i=2end current i=2start原创 2021-01-25 20:17:46 · 292 阅读 · 0 评论 -
go-break
breakdemo1for i:=0;i<10;i++ { if i>5 { break; } fmt.Printf("current i=%d \n",i) }输出current i=0current i=1current i=2current i=3current i=4current i=5例子2退出内循环func breakdemo2() { for i:=0;i<5;i++ { for j:=0;j<5;j++ {原创 2021-01-25 20:03:35 · 249 阅读 · 0 评论 -
go-defer
deferdefer用于延迟调用指定函数,defer只能出现在函数内部,因为defer的延迟特点,可以把defer语句用于回收资源、清理资源等简单例子func demo1() { defer fmt.Printf("defer execute. \n") fmt.Printf("welcome to golang ! \n")}输出welcome to golang !defer execute.例子2var i = 0func printI() { fmt.Printf原创 2021-01-25 19:51:13 · 180 阅读 · 0 评论 -
go-流程控制
ifif else a :=101 if a<10 { fmt.Printf("a < 10 \n") }else { fmt.Printf("a >= 10 \n") }if - else if - else a :=88 if a >= 100 { fmt.Printf("a > 100 \n") }else if a >= 80 { fmt.Printf("a >= 80 and a < 100 \n原创 2021-01-22 22:34:16 · 206 阅读 · 0 评论 -
go指针
声明// &name:指针地址 name:指针所指向对象的地址var name *type打印地址 num :=10 fmt.Printf("num.address=%x",&num)输出num.address=c00000a0a0例子 num1 :=18; num1pointer := &num1; fmt.Printf("num1.address=%x \n",&num1) fmt.Printf("num1poin原创 2021-01-21 23:21:53 · 249 阅读 · 0 评论 -
基本数据类型
强制转化type_name(expression)例子 sum := 100 count :=6 // int average1 := sum/count // float32 average2 := float32(sum)/float32(count) fmt.Printf("average1 type =%T, value=%d \n",average1,average1) fmt.Printf("average2 type =%T, value=%f \n",aver原创 2021-01-20 23:41:31 · 223 阅读 · 0 评论 -
go-strconv
ParseInt函数定义func ParseInt(s string, base int, bitSize int) (i int64, err error) { }base : 进制 比如2进制是2、10进制是10 、 以此类推bitSize 预期数值的bit大小,用于数值上限限制,最终返回的还是int64类型2进制例子 strnum1 :="0101" toint,err1 := strconv.ParseInt(strnum1,2,32) fmt.Printf("toi原创 2021-01-20 23:01:37 · 298 阅读 · 0 评论 -
go-strings
基础数据str := "/usr/local/redis/bin/redis.sh"HasPrefix是否存在前缀字符串 // false fmt.Printf("start with /usr result=%t \n",strings.HasPrefix(str,"/usr1")) // true fmt.Printf("start with /usr result=%t \n",strings.HasPrefix(str,"/usr"))HasSuffix是否存原创 2021-01-17 22:49:56 · 263 阅读 · 0 评论 -
go-string
描述Go语言中,一个字符串是一个不可改变的UTF-8字符串列,一个Ascii码占用1个字节,其他字符根据需要占用2-4字节。 Ascii码字符串// 第1个字节str[0]// 第i个字节str[i-1]// 最后1个字节str(len(str)-1)demo1 str := "Go 你好!" fmt.Printf("str.length=%d \n",len(str)) for i:=0;i<len(str);i++ { // 输出ascii原创 2021-01-17 21:29:28 · 231 阅读 · 0 评论 -
go-reflect
导入包import "reflect"推导类型aa := 1800;fmt.Printf("aa type is =%s",reflect.TypeOf(aa))原创 2021-01-17 16:16:45 · 208 阅读 · 0 评论 -
Go常见的编译不通过
例子1. 类型不一致 var a int16 = 10; var b int32 = 10; if(a==b) { // 编译不通过 }2. 超出范围 var c int8; if(c==128) { //越界 最大127 }原创 2021-01-17 16:06:03 · 409 阅读 · 0 评论 -
go包名
规则包名是不能包含有小数点(.)的类似java的包名,在go中是不合法的package com.oracle; // go是不合法的 例子// src/mycom/eneity//. 用 _代替package mycom_entityimport "fmt"func init() { fmt.Printf("Employee init is called.")}func Empoyee()(name string,age int8,cname string) {原创 2021-01-17 15:29:13 · 693 阅读 · 0 评论 -
go自定义包
工程图函数权限函数大写字母开头,其他包可以访问该函数函数小写字母开头,同包可以访问,其他包不能访问util.goackage mycomimport "fmt"/** public函数-要大写字母开头 */func IsBlank(str string) bool { return isEmpty(str);}/** 小写字母开头 同包可以访问 其他包不能访问 */func isEmpty(str string) bool { if len(str)原创 2021-01-17 00:30:31 · 302 阅读 · 0 评论 -
golang函数访问权限
结论函数大写字母开头,其他包可以访问该函数函数小写字母开头,同包可以访问,其他包不能访问utilpackage mycomimport "fmt"/** public函数-要大写字母开头 */func IsBlank(str string) bool { return isEmpty(str);}/** 小写字母开头 同包可以访问 其他包不能访问 */func isEmpty(str string) bool { if len(str) >0 {原创 2021-01-17 00:21:56 · 797 阅读 · 0 评论 -
go-os
import "fmt"import "os"var ( HOME = os.Getenv("HOME") USER = os.Getenv("USER") GOROOT = os.Getenv("GOROOT") PATH = os.Getenv("PATH"))fmt.Printf("home=%s,user=%s,goroot=%s,path=%s",HOME,USER,GOROOT,PATH)原创 2021-01-16 23:00:03 · 232 阅读 · 0 评论 -
fmt.Printf
fmt.Printfprint string (%s)fmt.Printf("name is %s \n",name0)print number (%d、%f)# %d 打印10进制fmt.Printf("num3=%d \n",num3)# %f打印浮点类型fmt.Printf("pi=%f \n",pi)print bool (%t)# %tvar limit bool = false;fmt.Printf("limit=%t \n",limit)...原创 2021-01-16 21:38:43 · 690 阅读 · 0 评论 -
go数据类型
Boolean typesA boolean type represents the set of Boolean truth values denoted by the predeclared constants true and false. The predeclared boolean type is bool; it is a defined type.Numeric typesA numeric type represents sets of integer or floating-翻译 2021-01-16 20:33:57 · 242 阅读 · 1 评论 -
GoLand 错误集锦
错误1Error:CreateProcess error=216错误信息Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容。请查看计算机的系统信息,然后联系软件发布者解决方法把包名改成main package main原创 2021-01-16 20:06:09 · 360 阅读 · 1 评论 -
go常量const
# [type] 可以省略# go语言编译器可以推断出值类型const identifier [type] = value# 不指定类型const pi = 3.1415926# 指定类型const pi float32 = 3.1415926const pi = 3.1415926const pif float32 = 3.1415926 const Return = true const mes = "welcome to golang" const message ...原创 2020-06-22 00:02:22 · 315 阅读 · 0 评论 -
Main file has non-package
刚接触golang,用gogland运行的时候报这个错:Error: Run after build is not possibleMain file has non-package or doesn't contain main functionpackage helloimport "fmt"func main() { fmt.Println("welcome to golang")}解决方法把hello包改成main包package mainimport原创 2020-06-21 23:46:46 · 378 阅读 · 0 评论 -
golang第一个demo
hello.gopackage mainimport "fmt"func main() { fmt.Println("welcome to golang !")}# 运行go run hello.go原创 2020-06-21 19:01:04 · 438 阅读 · 0 评论 -
go环境变量
$GOROOT表示Go语言在计算机上的安装路径$GOPATH这是Go语言的工作目录,可以有多个,类似于工作空间原创 2020-06-21 10:54:13 · 263 阅读 · 0 评论 -
windows安装golang
1. 进入下载界面https://golang.google.cn/dl/2. 选择windows版本下载3. 安装默认安装在C:\Go\,我这里更改安装路径,装到D:\Go\4. 验证go version...原创 2020-06-21 10:38:38 · 959 阅读 · 0 评论 -
golang.org不能访问解决方法
无法访问的golang.org,可以通过https://golang.google.cn来访问转载 2020-06-21 10:26:37 · 1816 阅读 · 0 评论