Outils pour utilisateurs

Outils du site


blog

Go lang - template - gotemplate

// Source - https://stackoverflow.com/a/59083271
// Posted by wasmup, modified by community. See post 'Timeline' for change history
// Retrieved 2026-05-03, License - CC BY-SA 4.0
 
package main
 
import (
    "fmt"
    "os"
    "text/template"
)
 
func main() {
    name := "AlphaGo"
 
    fmt.Printf("I am %s\n", name)
 
    t := template.Must(template.New("my").Parse("I am {{.Name}}\n"))
    t.Execute(os.Stdout, struct{ Name string }{name}) // I am AlphaGo
 
    t2 := template.Must(template.New("my").Parse("I am {{.name}}\n"))
    t2.Execute(os.Stdout, map[string]string{"name": name}) // I am AlphaGo
 
    t3 := template.Must(template.New("my").Parse("I am {{.}}\n"))
    t3.Execute(os.Stdout, name) // I am AlphaGo
}

FIXME

2026/05/03 16:06 · Jean-Baptiste

Go lang - if x in list

Source : https://stackoverflow.com/questions/15323767/does-go-have-if-x-in-construct-similar-to-python

Since Go 1.18 or newer, you can use slices.Contains.

func main() {
        liste1 := []string{"Meow", "Waf", "Moo"}
 
        if !slices.Contains(liste1, "Waff") {
                fmt.Println("Aucun chien trouvé")
        } else {
                fmt.Println("Un chien a été trouvé")
        }
}

Before Go 1.18 there was no built-in operator. You needed to iterate over the array. You had to write your own function to do it, like this:

func stringInSlice(a string, list []string) bool {
    for _, b := range list {
        if b == a {
            return true
        }
    }
    return false
}

FIXME

2026/04/29 18:42 · Jean-Baptiste

Pascal Lazarus - Compilation croisée dans un conteneur

Création de l'image conteneur

git clone --depth=1 https://github.com/ChrisWiGit/lazarus-docker
cd lazarus-docker
podman build -t lazarus-docker .

Paramètre de compilation

cat > runenv.sh <<'EOF'
#! /bin/bash
cd /project
lazbuildl64 *.lpr && install -d build/linux64/ && mv project1 /project/build/linux64/
lazbuildw32 *.lpr && install -d build/win32/ && mv project1.exe /project/build/win32/
lazbuildw64 *.lpr && install -d build/win64/ && mv project1.exe /project/build/win64/
EOF
 
chmod +x runenv.sh 

Compilation

podman run -v $PWD:/project --rm localhost/lazarus-docker:latest /project/runenv.sh

Suppression des symboles et info de debug pour réduire la taille des fichiers :

strip build/*/*
2026/04/27 23:31 · Jean-Baptiste

script - subshells sous-shells - comportement déroutant de bash qu'un débutant doit connaître

Exemple variable globale

Exemple bash de problème d'affectation de variable globale

test_gvar1.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
echo $(func_1)
echo $VAR
$ ./test_gvar1.sh
0
Hello
0

Alors que

test_gvar2.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
func_1
echo $VAR
$ ./test_gvar2.sh
0
Hello
4

Dans le 1er exemple la variable n'est pas affectée par la fonction. En effet avec $(nom_de_fonction) bash lance un autre bash et copie la fonction, puis retourne le résultat au bash père.

De cette manière il est impossible par exemple d'implémenter un compteur qui compterait le nombre de fois que la fonction est appelée.

Bien sûr il est possible à la place de faire :

echo "Fonction func appelée à $(date --rfc-3339=second)" >> /tmp/trace.log

Mais ce n'est pas terrible

Voici une solution :

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
echo "${ func_1; }"
echo $VAR

Autre solution : Utiliser les pointeurs en bash !

Voir : https://stackoverflow.com/questions/75493732/how-to-modify-a-global-variable-within-a-function-and-return-a-boolean-in-bash

#! /bin/bash
 
VAR=0
 
func_1() {
    declare -n -x VAR_ref=$1
    VAR_ref=4
}
 
echo $VAR
func_1 VAR
echo $VAR
Exemple de commande exit qui semble ignorée

test_exit1.sh

#! /bin/bash
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script

Il est possible de remplacer echo $(funcB) par echo ${ funcB; } mais la sortie de la fonction n'est pas affichée sur la sortie standard

$ ./test_exit1.sh
A
B
Suite du script
$ echo $?
0

Alors que :

test_exit2.sh

#! /bin/bash
 
funcA() {
        echo A
        funcB
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script
$ ./test_exit2.sh
A
B
$ echo $?
13

Exemple solution contournement

Une solution simple consiste à ajouter un trap et de remplacer la commande exit par la commande kill $$

test_exit3.sh

#! /bin/bash
 
trap 'exit 13' SIGUSR1
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        # exit 13
        kill -s SIGUSR1 $$
}
 
 
funcA
 
echo Suite du script
$ ./test_exit3.sh
A
B

Autres

Affecter une variable sans créer de sous-shell

echo 'hello' | { read msg; echo "$msg"; }
2026/04/25 11:16 · Jean-Baptiste

Go lang - compiler pour windows i386 32bits

Voir aussi :

sudo apt-get install gcc-multilib-i686-linux-gnu
 
export GOROOT=$HOME/opt/go-legacy-win7/
export PATH=$HOME/opt/go-legacy-win7/bin/:"$PATH"
 
env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CGO_LDFLAGS="-lssp" go build -ldflags="-s -w" -o app1.exe main.go

Ou encore avec zig

env CGO_ENABLED=1 GOROOT=~/opt/go-legacy-win7/ GOOS=windows GOARCH=386 CC="zig cc -target i686-linux-musl" ~/opt/go-legacy-win7/bin/go build hello.go

FIXME: i686-linux-musl n'est pas cohérent avec GOOS

Test

mkdir -p ~/myapp/prefix
export WINEPREFIX=$HOME/myapp/prefix 
export WINEARCH=win32 
export WINEPATH=$HOME/myapp 
wineboot --init
 
env WINEARCH=win32 wine app1.exe
$ exiftool app1.exe 
ExifTool Version Number         : 13.50
File Name                       : app1.exe
Directory                       : .
File Size                       : 1266 kB
File Modification Date/Time     : 2026:04:23 19:18:49+02:00
File Access Date/Time           : 2026:04:23 19:19:08+02:00
File Inode Change Date/Time     : 2026:04:23 19:18:49+02:00
File Permissions                : -rwxrwxr-x
File Type                       : Win32 EXE
File Type Extension             : exe
MIME Type                       : application/octet-stream
Machine Type                    : Intel 386 or later, and compatibles
Time Stamp                      : 0000:00:00 00:00:00
Image File Characteristics      : Executable, No line numbers, No symbols, 32-bit, No debug
PE Type                         : PE32
Linker Version                  : 2.45
Code Size                       : 571904
Initialized Data Size           : 693248
Uninitialized Data Size         : 154624
Entry Point                     : 0x1460
OS Version                      : 6.1
Image Version                   : 1.0
Subsystem Version               : 6.1
Subsystem                       : Windows command line

FIXME

2026/04/23 19:32 · Jean-Baptiste
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki