Categories
Programming Technology

Masoud’s notes about Golang part 4 – Golang Commands

Execute project

go run .

Create an executable file of the project (build)

go build .

Download and install external package

go get {package path}

#### eg.
go get gorm.io/gorm

Display Current GOPATH

go env GOPATH

install new go package

go install test-app

Update package

go get -u test-app

List Packages Alongside Dependencies

go list
go list ./...

Display Version Information

go version

Maintain Module Dependencies

The go mod tidy command allows us to add missing dependencies or delete unused modules. This go command helps in maintaining module dependencies and keep your applications as lean as possible. Simply run the following simple command to do this.

Make sure to run this from your project’s root directory. You can check the go.mod file to see which dependencies were added or deleted. The -v flag, if used, causes tidy to display which modules were removed to the standard error.

go mod tidy [-v]

Verify Module Dependencies

go mod verify

Display Why Packages/Modules are Needed

You can see why certain modules or packages are required in your application. This is useful if you are working with someone else’s code or are trying to figure out what certain things do in a specific project. The “why” command of the mod tool allows us to do this

The first one is the general syntax, and the second one is an example. It prints out why the language and encoding packages are required in your application.

go mod why [-m] [-vendor] packages...
go mod why golang.org/x/text/language golang.org/x/text/encoding

Categories
Programming Technology

Masoud’s notes about Golang – Part 2

when you want to run or build a go file it needs to be the main package (I’ll write more about packages in go later).

let’s div into example code

package main
import "fmt"

func main(){

	for i := 0 ; i <= 500 ; i++{
		s := ""
		
		if i % 7 == 0{
			s = "Haftaeiash"
		}
		if i % 3 == 0{
			s = "Setayi"
		}
		if s != "" {
			fmt.Println(s)
		}else{
			fmt.Println(i)
		}
	}
}

To run package :

go run masoud.go

To check out environments :

go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/masoud/.cache/go-build"
GOENV="/home/masoud/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/masoud/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/masoud/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3281051355=/tmp/go-build -gno-record-gcc-switches"

By default it prints the list as a shell script; however, if one or more variable names are given as arguments, it prints the value of each named variable.

$go env GOOS GOPATH
linux
/home/yourname
Categories
Programming Technology

Masoud’s notes about Golang part 1

In this series article, I want to note some tips during watching Golang courses that are important for me.

Overall points :

  • golang is a compiled programming language
  • like as c and c++ it needs a main function
  • all built in functions are not available like as php and you need to import situble library
  • you cant define unused variable , function or even import unused library.
  • to print we need to import “fmt” library
  • it needs to specify variable type on definitions

some usefull commands:

go mod init mymodule

go run main.go

Golang Data types list

Masoud Hosseini | golang data types

Array and slice in golang

#array : 
var booking [50]string
or 
var booking = [50]string{"masoud"}

#slice 
var booking []string
or istead of "var" we can use ":="
booking :=[]string

add an index to a slice

append(booking, firstName + "- "+ lastName)

loop in Golang

we have just “for” loop in golang

to ignore a variable you dont want to use you can use “unser line” => “_”

so with Go you need to make unused variables explicit

for example :

firstNames := []string{}
		for _, bookingItem := range booking {
			var names = strings.Fields(bookingItem)
			firstNames = append(firstNames, names[0])
		}