Useful GoLang tips.
While refactoring and troubleshooting code it is sometimes necessary to point to the local version of a package.
You can do this by modifying the go.mod
file as follows:
require (
github.com/queone/utl v1.0.0
)
replace github.com/queone/utl => /Users/myuser/mycode/utl
$ go build
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Try this
go mod init <package_name> # For example, this would be 'zls' for github.com/git719/zls
go mod tidy
go clean -modcache
staticcheck
and run:
go vet ./...
staticcheck ./...
This will check for common code issues.
Use the install-golang.sh
BASH script
curl -LO https://raw.githubusercontent.com/git719/tools/main/bash/install-golang.sh
to download the script./install-golang.sh 1.22.0
to install GoLang version 1.22.0
$HOME/go
, so if you have a previous version there you will first need to back that upGOPATH
variable to ~/go
; add it in your PATH
; and also add $GOPATH/bin
to itThis script can be used to install Go on:
shasum
command is in package perl-Digest-SHA
)To reduce binary executable sizes:
-ldflags "-s -w"
brew install upx
upx -9 <binary>
func FieldInStruct(Field string, Struct interface{}) bool {
val := reflect.ValueOf(Struct)
for i := 1; i < val.Type().NumField(); i++ {
if val.Type().Field(i).Name == Field {
return true
}
}
return false
}
func GetListFromLocalFile(storeFile string) interface{} {
localFile := filepath.Join(progConfDir, storeFile) // Note progConfDir is global
JSONData, err := ioutil.ReadFile(localFile)
if err != nil {
panic(err)
}
switch storeFile {
case InstanceDataFile: // Return list of instance records
var list []InstanceType
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
case DNSDataFile: // Return list of DNS records
var list []ResourceRecordSetType
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
case ELBDatafile: // Return list of ELB records
var list []LoadBalancerDescriptionType
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
case ZoneDataFile: // Return list of zone records
var list []HostedZoneType
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
case StackDataFile: // Return list of stack records
var list []StackType
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
default: // Return list of generic JSON records
var list interface{}
err = json.Unmarshal(JSONData, &list)
if err != nil { panic(err) }
return list
}
}
Makefiles are usually not needed with Go, but if you must, this one for macOS and Linux will build target binaries for multiple OSes.
# Makefile
# Assumes GOPATH is already set up properly, e.g., $HOME/go
default:
GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o build/macos/awsinfo
all:
rm -rf build
mkdir -p build/{macos,centos,windows}
go get -u github.com/aws/aws-sdk-go/...
go get -u github.com/vaughan0/go-ini
GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o build/macos/awsinfo
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o build/centos/awsinfo
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o build/windows/awsinfo.exe
# Modify below target to where you keep your binaries
install:
cp build/macos/awsinfo $(HOME)/data/bin
clean:
rm -rf build