Golang (Application)

From campisano.org
Jump to navigation Jump to search

Golang

Install

Using golang binaries

# shared/opt install schema v1.5.6

#### as common user ####
# define applications vars
export SOFTWARE_PATH="/home/shared/opt/software"
export NAME="golang"
export VERSION="1.16.5"
export URL="https://golang.org/dl/go${VERSION}.linux-amd64.tar.gz"
su - -w SOFTWARE_PATH,NAME,VERSION

#### as root ####
# install packages and prepare destination path
apt-get autoremove --purge golang\* golang-docker-credential-helpers+
apt-get -q -y install wget coreutils findutils < /dev/null
apt-get -q -y install tar gzip < /dev/null
mkdir -m 777 "${SOFTWARE_PATH}/tmp_install/" "${SOFTWARE_PATH}/${NAME}_${VERSION}/"
exit

#### as common user ####
umask 0027
cd "${SOFTWARE_PATH}/tmp_install"
wget -c --no-check-certificate "${URL}"
tar -xzf "go${VERSION}.linux-amd64.tar.gz"
cd "go"
mv * "${SOFTWARE_PATH}/${NAME}_${VERSION}"
cd
su - -w SOFTWARE_PATH,NAME,VERSION

#### as root ####
# ensure permissions to destination path
cd "${SOFTWARE_PATH}"
chown -R root:users "${NAME}_${VERSION}"
find "${NAME}_${VERSION}" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "${NAME}_${VERSION}" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
rm -rf tmp_install
ln -s -f -T "${NAME}_${VERSION}" "${NAME}"
exit

#### as common user ####
# test the application (you can put the follow in ~/.profile)
export SOFTWARE_PATH="/home/shared/opt/software"
export GOROOT="${SOFTWARE_PATH}/golang"
export GOPATH="${HOME}/.golang"
export GOPROXY="direct"
export GOSUMDB="off"
export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin"
go version

Using system packages

  • just a package:
su -
#### as root
# cleanup
apt-get autoremove --purge golang\*
## from Debian STABLE:
sudo apt-get install golang # Stable package
## or Debian BACKPORT:
sudo apt-get -t stretch-backports install golang
## to DEBUG, install also gdb:
sudo apt-get install gdb
#
exit
  • and setup your user environment (you can put the follow in ~/.profile)
export GOROOT="/home/shared/opt/software/golang"        # Edit when needed
export GOPATH="${HOME}/.golang" 
export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin" 

Test

mkdir -p "$HOME/tmp"
cd "$HOME/tmp"
go mod init example.com/hello
cat > hello.go <<EOF
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
EOF
go build
./hello
# hello, world

Memory

In go1.19 (released 2022-08-02), it is possible to control the memory usage with the GOMEMLIMIT env var.

That's why in the 1.19 release, Go added support for setting a runtime memory limit. The memory limit may be configured either via the GOMEMLIMIT environment variable which all Go programs recognize.
...
* Do take advantage of the memory limit when the execution environment of your Go program is entirely within your control, and the Go program is the only program with access to some set of resources (i.e. some kind of memory reservation, like a container memory limit).

A good example is the deployment of a web service into containers with a fixed amount of available memory. 

Note about the GOGC env var, useful to tune the garbage collection aggressiveness:

As an example, consider a Go program with a live heap size of 8 MiB, 1 MiB of goroutine stacks, and 1 MiB of pointers in global variables. Then, with a GOGC value of 100, the amount of new memory that will be allocated before the next GC runs will be 10 MiB, or 100% of the 10 MiB of work, for a total heap footprint of 18 MiB. With a GOGC value of 50, then it'll be 50%, or 5 MiB. With a GOGC value of 200, it'll be 200%, or 20 MiB.

Troubles

In case of dependencies problems, e.g.

go: verifying github.com/labstack/echo@v3.3.5+incompatible: checksum mismatch

clean the checksum file:

rm go.sum
go clean -modcache

Source: go/issues/29281

IDEs

GoClipse

  • NOTE, the project is outdated

1) add goclipse plugin from http://goclipse.github.io/releases/

2) configure and install gocode, guru, godef tools

go get -u github.com/nsf/gocode
go get -u golang.org/x/tools/cmd/guru
go get -u github.com/rogpeppe/godef

3) configure build target using a Makefile, and run it from project_folder->right_click->build

make -C ${project_loc} debug

4) add a simple Makefile, like

debug:
    go build -v -gcflags=all="-N -l" ./cmd/YOUR_PACKAGE

LiteIDE

Install

shared/opt install schema v1.2

su -
#### as root
# dependencies
apt-get install curl tar gzip qt5-default gcc g++ gdb
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="x36.2"                                         # Edit when needed
URL="https://github.com/visualfc/liteide/archive/${VERSION}.tar.gz"
umask 0027
mkdir -p "${DESTINATION}/tmp"
cd "${DESTINATION}/tmp"
curl -C - -kLO "${URL}"
tar -xzf "${VERSION}.tar.gz"
cd "liteide-${VERSION}/build/"
chmod -R 777 ../
su -c "/bin/bash" USERNAME
#### as common user
./update_pkg.sh
./build_linux_qt5.sh
./deploy_linux_x64_qt5.sh
exit
#### as root
cd "${DESTINATION}"
chown -R root:users "tmp"
find "tmp" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "tmp" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
mv "tmp/liteide-${VERSION}/build/liteide" "liteide_${VERSION}"
rm -rf tmp liteide
ln -s "liteide_${VERSION}" liteide
exit
#### as final user
SOFTWARE_PATH="/home/shared/opt/software"               # Edit when needed
"${SOFTWARE_PATH}/liteide/bin/liteide"

GoWorks (NetBeans)

NOTE - OUTDATED SINCE 2013

Emacs

;; from https://github.com/dominikh/go-mode.el
(use-package go-mode
  :ensure t
  :config
  (autoload 'go-mode "go-mode" nil t)
  (add-to-list 'auto-mode-alist '("\\.go\\'" . go-mode))
  )

;; from https://github.com/mdempsky/gocode/tree/master/emacs-company
;; remember to install gocode with
;;   go get -u github.com/mdempsky/gocode
(use-package company-go
  :ensure t
  :config
  (add-hook 'go-mode-hook
            (lambda()
              (set (make-local-variable 'company-backends) '(company-go))
              (company-mode)))
  )

;; from https://github.com/benma/go-dlv.el/blob/master/go-dlv.el
;;      http://tleyden.github.io/blog/2014/05/22/
;;                               configure-emacs-as-a-go-editor-from-scratch/
;; remember to install dlv with
;;   go get -u github.com/derekparker/delve/cmd/dlv
(use-package go-dlv
  :ensure t
  )

;; defining a custom layout for DLV
;; from http://www.campisano.org/wiki/en/Emacs
(defun fun_set-dlv-layout(var_editing_buffer)
  ;; (defvar-local var_source_window (selected-window))
  ;; (defvar-local var_debugging_window                     ; bottom
  ;;   (split-window var_source_window (floor(* 0.66 (window-body-height)))
  ;;                 'below))

  ;; (set-window-dedicated-p var_debugging_window t)
  ;; (select-window var_source_window)
  (set-window-buffer var_source_window var_editing_buffer)
  )

;; (defvar global-config-editing)
(global-set-key (kbd "C-c g")
                (lambda() (interactive)
                  ;; (setq global-config-editing (current-window-configuration))
                  (setq var_editing_buffer (window-buffer (selected-window)))
                  ;; (dlv (concat "debug " (buffer-file-name)))
                  (dlv-current-func)
                  (fun_set-dlv-layout var_editing_buffer)
                  ;; (set-window-configuration global-config-editing)
                  ))