Wednesday, December 15, 2021

go tools

 As developer we constantly use tools that let us go faster in our daily tasks, not only go faster also we have to write code with a good quality, because we need to write thinking in the next developer that will have to touch the code, so always choose explicit instead implicit, keep the things simple and Try to not repeat yourself (DRY). In this long way we always continue learning and improving our way of writing code in Go language, some of this tools  I listed here:


go get / go install (Go tools)


This cli command lets you get a dependency and install it  in your GO_PATH. In the last version of go (1.17.0), “go get/install” was redesigned with modules, and improved usability of it, which will help speed up the process of getting users up to speed.[2] 


With go get your will update your dependency and edit go.mod and go.sum. In the best practice of golang they advise to generally not try to consume or edit go.mod and go.sum directly because this should be timeless for the foreseeable future, also it's hard for a human to fully understand the dependency graph by reading a go.mod file. Also with go get you could remove dependencies, the command looks like “go get example.com/theirmodule@none”[2]


Using go tools to get your dependencies your requirements remain consistent and the content of your go.mod file is valid. If you need to search for packages you might find useful, you should query here: https://pkg.go.dev/


“Go mod init “ lets you initialize a new module, and when you are using conventions this ensures it’s available to others via Go tools. Each directory is considered its own package, and each file has its own package declaration line. One repository could have one module or multiple modules (decentralized publishing), the best practice is to have one module by repository because it’s better for upgrade, and maintenance is simpler.[1] 


A module could have many packages inside. When Go sees that a package is named main it knows the package should be considered a binary, and should be compiled into an executable file, instead of a library designed to be used in another program.  When Go sees a function named main inside a package named main, it knows the main function is the first function it should run. This is known as a program’s entry point. GOPATH is an environment variable where you define where the go binary is. Don't worry if you are not familiar with the GOPATH, because using modules replaces its usage.[3]


“go list -m all” list all dependencies(-m = modules ) with -u list the modules that need an upgrade  and  “go list -m -versions github.com/lib/pq” list all versions for the lib/pq module. “go mod tidy”: is used for removing unnecessary or unused dependencies.“go install”, let you install the application locally.


GVM (go version manager) [5]

require bison package at least, to install this:  sudo apt-get install bison or sudo pacman -S bison

The easiest way of install this tools, is with the script installer, you could use bash or zsh:


The output should looks like: Cloning from https://github.com/moovweb/gvm.git to /home/jennifer/.gvm
Created profile for existing install of Go at /usr/local/go
Installed GVM v1.0.22
Please restart your terminal session or to get started right away run
`source /home/jennifer/.gvm/scripts/gvm`

Gvm commands:

➜  ~ gvm list all # to list all go version on my laptop
➜  ~ gvm install go1.14.1 # to install a new go version
➜  ~ gvm use go1.17.4 # to use a go version in the current terminal session



➜  ~ cd new_project #to use many gopath directories, new_project will be added to GO_PATH
➜  ~ gvm pkgset create -local
➜  ~ gvm pkgset use --local 


Ayu [6]

Ayu is an extension for visual studio code IDE, this extension implements three themes: Ayu Dark, Ayu Light and Ayu Mirage, this themes lets you highlight the go syntax, as I spent long hours with IDE on my screen it’s comfortable to my eyes to have set Ayu Mirage as theme.


Golines

Golines is a go app that lets you short the length of lines in a file or in all the files in a directory. To install:  go install github.com/segmentio/golines@latest and to use:  golines [paths to format] you could configure this command in visual studio code to be executed every time you save the file (Run on Save plugin [7]).

Go test and  Go mock

Is part of the Go language core so you do not need to define your own interface and mock classes. You do not need to depend on a third party library. The creators of Go foresaw the need to mock outbound HTTP requests a long time ago, and included an API in the standard library.  The API is provided in the package httptest, and there are many examples of how to use it, including not only in the httptest package’s own Godoc example. Unit tests are useful for the maintenance of code and checking if the application it’s ok after we upgrade our dependencies, and let us make sure our packages and modules are working correctly. “go test all” runs the tests of all direct and indirect dependencies of your module, which is one way to validate that your current combination of versions work together[8][9]. If you are curious as to why a particular module is showing up in your go.mod, you can run “go mod why -m <module>” to answer that question. Other useful tools for inspecting requirements and versions include “go mod graph”

Some recommendations: when you have to update an API in a golang module you should create a new path for the new version in case the contract API is modified in some parameters. Choose to use interfaces to make a contract for Request and Response API’s, reserve time to do unit tests, and functional tests with a coverage over 70%, use “go test -v -cover” to check this[10]. With these tools and taking into account these recommendations we built better software, that will keep back compatibility with older versions, and make for the next developers an easy task or more comfortable the maintenance of the project.


[1] https://go.dev/doc/modules/managing-source

[2] https://go.dev/doc/modules/managing-dependencies

[3] https://go.dev/doc/modules/developing#workflow

[4] https://go.dev/blog/module-compatibility

[5] https://github.com/moovweb/gvm

[6] https://github.com/ayu-theme/vscode-ayu

[7] https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave

[8] https://github.com/golang/go/wiki/Modules#faqs--gomod-and-gosum

[9] https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies

[10] https://gitlab.intraway.com/jennifer.maldonado/gpon-ipv4-simulator-config/tree/master/dhcp_templates


PD: sorry if there are any typos or grammar mistakes. Suggestions for corrections are accepted.

Tuesday, December 14, 2021

avoiding CORS ups..

bad practices

How  to  avoid CORS error on google-chrome-stable version >= 96.0.4664.93

1 - execute google chrome by cli in this way:

google-chrome --disable-web-security --user-data-dir=/tmp


2 - access to this URL in the new chrome opened: chrome://flags/#reduced-referrer-granularity

and disable web security.

search by: 

"

Block insecure private network requests.

Prevents non-secure contexts from making sub-resource requests to more-private IP addresses. An IP address IP1 is more private than IP2 if 1) IP1 is localhost and IP2 is not, or 2) IP1 is private and IP2 is public. This is a first step towards full enforcement of CORS-RFC1918: https://wicg.github.io/cors-rfc1918 – Mac, Windows, Linux, Chrome OS, Android


"



See Also:

- https://developers.google.com/web/updates/2020/07/referrer-policy-new-chrome-default
- https://chromestatus.com/feature/6251880185331712