go 工程搭建

go 路径问题: go install 下载到$GOPATH/bin 下生成exe,$GOPATH/bkg下载包

 

cd 到目标文件目录,go install,

cd $GOPATH/bin

./main.exe

 

go get 对比 go installhttps://juejin.cn/post/7223241078942613564

 

Key Differences

 

Featurego getgo install
Purpose Fetches and installs packages and dependencies Compiles and installs a Go package as a binary
Modifies go.mod Yes, updates dependencies No, does not modify go.mod
Usage Adding or updating dependencies Installing binaries
Output Downloads source code and updates module files Builds and installs binary in $GOPATH/bin or $GOBIN
Typical Use Managing dependencies Installing Go command-line tools
  • go get: Use this command for managing your project's dependencies, adding new packages, and updating existing ones. It modifies the go.mod file to reflect these changes.
  • go install: Use this command to compile and install Go packages as binaries, especially useful for installing command-line tools. It does not affect your project's dependency files.

 

goland 执行main.go,编译到哪里?如何执行?

C:/Users/Administrator/appData/Local/Temp 生成临时编译文件

go install & go build 区别:https://juejin.cn/post/6844903938060222471

go 编译成linux形式

go env -w CGO_ENABLED=0 GOOS=linux GOARCH=amd64 
go build main.go 

Go build

#Mac下编译Linux, Windows平台的64位可执行程序:
$ go env -w CGO_ENABLED=0 GOOS=linux GOARCH=amd64
$ go env -w CGO_ENABLED=0 GOOS=windows GOARCH=amd64
#Linux下编译Mac, Windows平台的64位可执行程序: $ go
env -w CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $ go env -w CGO_ENABLED=0 GOOS=windows GOARCH=amd64
#Windows下编译Mac, Linux平台的64位可执行程序: $ go
env -w CGO_ENABLED=0 GOOS=darwin3 GOARCH=amd64 $ go env -w CGO_ENABLED=0 GOOS=linux GOARCH=amd64
#执行编译 go build
-o "你要存储编译后的文件到哪里?" main.go

manage dependencies

//package 组织源码,go源码的集合,复用代码
// same directory same package
//一般是目录名和包保持一致

import (
"fmt"
n "learngo/ch1/user"
_ "learngo/ch2/user" // use to init() to print log
)

//install

1. IDE sync the package
2. cd 到目录
go get github/.../v8
go get -u (update to the latest)
go get github/.../v8@version // version


go mod help
go mod graph // dependencies
go mod tidy // clean and organize

go install
error=> unresolved reference: File->invalidate caches

go edit -replace

 go edit -replace github/.../A=github/.../project-A@v1.0.0

regulation

1. naming
package:
1.same as directory
2.meaningful
3.lowercase

file:
1.user_name.go // lowercase

variable:
1. userName
2. UserName
3. un
4. URLVersion
5. (bool) has is can

struct:User
interface:
-er: Writer
IRead
const: APP_VERSION // UPPERCASE

2. comment // /**/

note the sequence of importing

import(
"os" // system

"gorm.io/gorm" // third-party

"src.imooc/bobby/A" inner
)

unit test

go test: in the package, run all files ended with _test.go
Test has 4 categories:
1) Test_ for functionality
2) Benchmark for performance
3) example
4) ambiguous test

1> demo

 

package ch11

import "testing"

func Test_add(t *testing.T) {
    res := add(1, 3)
    if res != 4 {
        t.Errorf("expect %d, actual get %d", 4, res)
    }
}

also can cd to the ch11 and run "go test"

2> how to skip time-consuming test

 purpose: skip Test_add2()

func Test_add2(t *testing.T) {
    fmt.Println("111111")
    if testing.Short() {
        fmt.Println("22222")
        t.Skip("skippppppp")
    } // this is the end
.... // not reach the code here

 
3> Table driven test unit: 当我不想调用gorm去连接数据库,想直接测试。可以提前把数据录入进表格。(设置期望)

func Test_add3(t *testing.T) {
    var u = []struct {
        a   int
        b   int
        out int
    }{
        {1, 1, 2},
        {1, 1, 2},
        {2, 2, 4},
    }
    for _, value := range u {
        re := add(value.a, value.b)
        if re != value.out {
            t.Errorf("expect %d, actual get %d", value.out, re)
        }
    }
}

Benchmark

purpose: compare the performance of 3 kinds of way to construct string

const numbers = 10000
func
BenchmarkSprintf(bb *testing.B) { bb.ResetTimer() for i := 0; i < bb.N; i++ { var str string for j := 0; j < numbers; j++ { str = fmt.Sprintf("%s%d", str, j) } } bb.StopTimer() } func BenchmarkStringBuilder(bb *testing.B) { bb.ResetTimer() for i := 0; i < bb.N; i++ { var builder strings.Builder for j := 0; j < numbers; j++ { builder.WriteString(strconv.Itoa(j)) } _ = builder.String() } bb.StopTimer() } func BenchmarkStringAdd(bb *testing.B) { bb.ResetTimer() for i := 0; i < bb.N; i++ { var str string for j := 0; j < numbers; j++ { str = str + strconv.Itoa(j) } } bb.StopTimer() }

run  go test -bench=".*" 

Go Path vs Go Module

GO PATH: only build the code under the GOPATH/src can the project recognize 

otherwise, import "projectName/addFunc" is wrong(gopath下会先去gopath/src下找,再去goroot/src下找,找不到就报错,因为没做package management)

设置国内代理 :  go env -w GOPROXY="https://goproxy.cn,direct" 

change GOPATH project to module project:

GO111MODULE = OFF (PATH) / ON (MODULE) /AUTO(新版本默认)

 把project变成一个module,转换为module模式,import module

 

posted @ 2023-10-12 17:41  PEAR2020  阅读(7)  评论(0编辑  收藏  举报