Visualizzazione post con etichetta GoLang. Mostra tutti i post
Visualizzazione post con etichetta GoLang. Mostra tutti i post

venerdì 11 febbraio 2022

Ser2Net Go

 Premesso che questo non e' il sistema corretto di usare una goroutine in quanto non termina mai ho provato a fare questo programma che in una goroutine legge i valori da una porta seriale e tramite un channel manda i dati ad un'altra goroutine che invia sulla rete

non e' un codice da copiare

package main
import (
    "bufio"
    "fmt"
    "io"
    "log"
    "net"
    "time"

    "github.com/tarm/serial"
)

/*
per fare i test si puo' creare una porta seriale virtuale con
socat -d -d pty,raw,echo=0 pty,raw,echo=0
il sistema risponde con messaggi del tipo
socat -d -d pty,raw,echo=0 pty,raw,echo=0

2022/01/02 14:53:04 socat[18636] N PTY is /dev/pts/3
2022/01/02 14:53:04 socat[18636] N PTY is /dev/pts/4
2022/01/02 14:53:04 socat[18636] N starting data transfer loop with FDs [5,5] and [7,7]

si apre un altro terminale e si inviano i messaggi sulla seconda porta
echo "messaggio" > /dev/pts/4
il programma in Go deve andare in ascolto sull /dev/pts/3
*/

func main() {
    canale := make(chan []byte, 1024)

    usbRead := &serial.Config{Name: "/dev/pts/3", Baud: 9600}
    //s, err := serial.OpenPort(c)
    port, err := serial.OpenPort(usbRead)
    if err != nil {
        log.Fatal(err)
    }
    scanner := bufio.NewScanner(port)
    go scrivi_channel(canale, scanner)

    listener, err := net.Listen("tcp", "localhost:8000")
    if err != nil {
        log.Fatal(err)
    }
    for {
        conn, err := listener.Accept()
        if err != nil {
            log.Print(err) // e.g., connection aborted
            continue
        }
        handleConn(conn, canale) // handle one connection at a time
    }
}

func handleConn(c net.Conn, canale chan []byte) {
    defer c.Close()
    for {
        _, err := io.WriteString(c, string(<-canale)+" "+time.Now().Format("15:04:05\n"))
        if err != nil {
            return // e.g., client disconnected
        }
        time.Sleep(1 * time.Second)
    }
}

func scrivi_channel(canale chan []byte, scanner *bufio.Scanner) {
    buf := make([]byte, 128)
    scanner.Buffer(buf, 128)
    for {

        //scanner.Buffer(buf, 128)
        scanner.Scan()
        input := scanner.Text()
        fmt.Println(input)
        canale <- []byte(input)
        time.Sleep(1 * time.Second)
    }

    //time.Sleep(1 * time.Second)
    //canale <- []byte("test")
}

TicTacToe Go

 Volevo provare a creare una macchina di apprendimento per il gioco TicTacToe (Tris) 

Il passo e' stato quello di creare il gioco per umani...la cosa che mi ha dato piu' soddisfazione e' stata la funzione per determinare la condizione di vittoria dei giocatori

In pratica ogni casella del gioco e' associata ad una potenza di 2...si sommano i valori delle caselle e si fa un AND con la maschera di bit delle condizioni di vittoria

il prossimo passo e' salvare le varie partite con una Trie 

---------------------------

Il nome delle caselle e' cosi' ordinato

0 | 1 | 2
______
3 | 4 | 5
______
6 | 7 | 8



package main

import (
    "fmt"
    "math"
    "os"
    "strconv"
)

var display [9]int // array per mostrare il gioco
var step int       // numero di mossa
var giocatore int
var mossa_valida bool

func reset() {
    for k := 0; k < 9; k++ {
        display[k] = 0
    }
    step = 1
    giocatore = 3
    mossa_valida = false
}

func controlla_vittoria() int {
    totale_1 := 0
    totale_2 := 0

    // per il calcolo della scacchiera ad ogni casella corrispondente un valore di potenza di 2
    // si sommano tutti le caselle occupate da un giocatore
    // poi si effettua un AND logic con le 8 posizioni vincenti

    // giocatore 1
    for k := 0; k < 9; k++ {
        if display[k] == 3 {
            totale_1 = totale_1 + int(math.Pow(float64(2), float64(k)))
        }
    }

    if (totale_1 & 448) == 448 {
        return 1
    }
    if (totale_1 & 56) == 56 {
        return 1
    }
    if (totale_1 & 7) == 7 {
        return 1
    }
    if (totale_1 & 292) == 292 {
        return 1
    }
    if (totale_1 & 146) == 146 {
        return 1
    }
    if (totale_1 & 73) == 73 {
        return 1
    }
    if (totale_1 & 273) == 273 {
        return 1
    }
    if (totale_1 & 84) == 84 {
        return 1
    }

    // giocatore 2
    for k := 0; k < 9; k++ {
        if display[k] == 2 {
            totale_2 = totale_2 + int(math.Pow(float64(2), float64(k)))
        }
    }

    if (totale_2 & 448) == 448 {
        return 2
    }
    if (totale_2 & 56) == 56 {
        return 2
    }
    if (totale_2 & 7) == 7 {
        return 2
    }
    if (totale_2 & 292) == 292 {
        return 2
    }
    if (totale_2 & 146) == 146 {
        return 2
    }
    if (totale_2 & 73) == 73 {
        return 2
    }
    if (totale_2 & 273) == 273 {
        return 2
    }
    if (totale_2 & 84) == 84 {
        return 2
    }

    return 0 // se ritorna 0 nessuno ha vinto
}

func casella(c int) string {
    switch c {
    case 0:
        return (" ")
    case 3:
        return ("X")
    case 2:
        return ("O")
    }
    return " "
}

func mostra_gioco() {
    fmt.Print(casella(display[0]))
    fmt.Print("|")
    fmt.Print(casella(display[1]))
    fmt.Print("|")
    fmt.Println(casella(display[2]))
    fmt.Println("-----")
    fmt.Print(casella(display[3]))
    fmt.Print("|")
    fmt.Print(casella(display[4]))
    fmt.Print("|")
    fmt.Println(casella(display[5]))
    fmt.Println("-----")
    fmt.Print(casella(display[6]))
    fmt.Print("|")
    fmt.Print(casella(display[7]))
    fmt.Print("|")
    fmt.Println(casella(display[8]))
}

func main() {

    var tasto int

    step = 1
    giocatore = 3
    mossa_valida = false
    mostra_gioco()
    fmt.Println("Turno al Giocatore " + strconv.Itoa(giocatore-2))

    for {
        for { // controlla se e' una mossa valida

            if !mossa_valida {
                fmt.Print("Enter move: ")
                fmt.Scanln(&tasto)
                // controlla se e' un tasto valido

                if display[tasto] == 0 { // controlla se la casella e' valida
                    mossa_valida = true
                    display[tasto] = giocatore
                    giocatore = ((giocatore + 1) % 2) + 2
                    fmt.Println("Turno al Giocatore " + string(giocatore+2))
                    break
                } else {
                    fmt.Println("Mossa non valida")
                }

            }
        }

        step++ // aumenta il numero della mossa
        mossa_valida = false
        mostra_gioco()
        v := controlla_vittoria()
        switch v {
        case 1:
            fmt.Println("****************************")
            fmt.Println("*** Vittoria Giocatore 1 ***")
            fmt.Println("****************************")
            reset()
            mostra_gioco()
            break
        case 2:
            fmt.Println("****************************")
            fmt.Println("*** Vittoria Giocatore 2 ***")
            fmt.Println("****************************")
            reset()
            mostra_gioco()
            break
        }
        if step == 9 {
            fmt.Println("***************************")
            fmt.Println("******** Pareggio *********")
            fmt.Println("***************************")
            reset()
            mostra_gioco()
            break
        }
    }
    os.Exit(0)
}

Rest client in GO

Un semplice di client di servizio Restful con GO


package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    client := http.Client{}
    req, err := http.NewRequest("GET", "http://xxxxxxxxxxxxxxxxx/api/v1/authentication/", nil)
    if err != nil {
        //Handle Error
    }

    req.Header = http.Header{
        "Host":                  []string{"http://xxxxxxxxxxxxxxxxxx/api/v1/authentication/"},
        "api-key":         []string{"axxxxxxxxxxxx"},
        "api-version":     []string{"v1"},
        "device-language": []string{"it"},
        "id-user-session": []string{"guest"},
        "Accept":                []string{"application/json"},
        "Authorization":         []string{"Basic Z2lvxxxxxxxxxxxxxxsaQ=="},
    }

    res, err := client.Do(req)

    if err != nil {
        fmt.Errorf("got error")
    } else {
        //fmt.Println("--------------------------")
        //fmt.Println(res)
    }
    defer res.Body.Close()

    b, err := ioutil.ReadAll(res.Body)
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Println(string(b))
}

venerdì 7 gennaio 2022

Ambiente di sviluppo Golang senza permessi di amministratore

 Per impostare un ambiente di lavoro Golang su Windows senza avere i permessi di amministrazione si inizia scaricando GO non in versione installer ma in versione archive del tipo  go1.17.6.windows-amd64.zip




Si decomprime la cartella nella propria home

A questo punto si impostano le variabili di ambiente per l'account aggiungendo alla PATH la directory appena scompattata puntando alla directory /bin

Dopo di cio' si crea una nuova variabile di nome GOPATH e la si fa puntare alla directory dove si intende effettuare lo sviluppo

Se si e' dietro ad un proxy si puo' settare la variabile GOPROXY



sabato 1 gennaio 2022

UI per Golang

 Go non ha una UI di default. Si possono utilizzare dei bindings con GTK3 o delle UI native 



go mod init luca.innocenti.gtk3test

si caricano i moduli

go get github.com/gotk3/gotk3/glib

go get github.com/gotk3/gotk3/gtk

si apre quindi Glade per creare la pgina (una semplice textbox Entry nel linguaggio di GTK, una label ed un pulsante)

---- completo.glade -----------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="main_window">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_top">4</property>
        <property name="margin_bottom">4</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkEntry" id="txt1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="lbl1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Etichetta</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="btn1">
            <property name="label" translatable="yes">Ok</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

---------------------------------------------

nel codice go si carica il file completo.glade

Si tratta di una piccola modifica del codice degli esempi che si trovano su GitHub 

Gli eventi possono essere creati (connessi) sia nel codice sia all;'interno dei segnali di Glade...in questo caso sono tutti contenuti nel codice applicazione. In ogni caso se si puo' associare in Glade un segnnale di nome "pressione" all'evento di click del pulsante

package main

import (
"errors"
"fmt"
"log"
"os"

"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)

const appId = "com.github.gotk3.gotk3-examples.glade"

func main() {
// Create a new application.
application, err := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
errorCheck(err)

// Connect function to application startup event, this is not required.
application.Connect("startup", func() {
log.Println("application startup")
})

// Connect function to application activate event
application.Connect("activate", func() {
log.Println("application activate")

// Get the GtkBuilder UI definition in the glade file.
// builder, err := gtk.BuilderNewFromFile("ui/example.glade")
builder, err := gtk.BuilderNewFromFile("/home/luca/glade/completo.glade")
errorCheck(err)

obj, _ := builder.GetObject("txt1")
entry1 := obj.(*gtk.Entry)

obj2, _ := builder.GetObject("btn1")
button1 := obj2.(*gtk.Button)

obj3, _ := builder.GetObject("lbl1")
label1 := obj3.(*gtk.Label)

button1.Connect("clicked", func() {
text, err := entry1.GetText()
if err == nil {
label1.SetText(text)
}
})

// Map the handlers to callback functions, and connect the signals
// to the Builder.
signals := map[string]interface{}{
"on_main_window_destroy": onMainWindowDestroy,
//"pressione": clickedTestButton,
}
builder.ConnectSignals(signals)

// Get the object with the id of "main_window".
obj4, err := builder.GetObject("main_window")
errorCheck(err)

// Verify that the object is a pointer to a gtk.ApplicationWindow.
win, err := isWindow(obj4)
errorCheck(err)

// Show the Window and all of its components.
win.Show()
application.AddWindow(win)
})

// Connect function to application shutdown event, this is not required.
application.Connect("shutdown", func() {
log.Println("application shutdown")
})

// Connect function to application shutdown event, this is not required.
application.Connect("pressione", func() {
log.Println("pressione")
fmt.Println("pressione")
})

// Launch the application
os.Exit(application.Run(os.Args))
}

func isWindow(obj glib.IObject) (*gtk.Window, error) {
// Make type assertion (as per gtk.go).
if win, ok := obj.(*gtk.Window); ok {
return win, nil
}
return nil, errors.New("not a *gtk.Window")
}

func errorCheck(e error) {
if e != nil {
// panic for any errors.
log.Panic(e)
}
}

/*
func clickedTestButton() {
fmt.Println("Testclick")
//headerBar.SetTitle("New Title!!")
}*/

// onMainWindowDestory is the callback that is linked to the
// on_main_window_destroy handler. It is not required to map this,
// and is here to simply demo how to hook-up custom callbacks.
func onMainWindowDestroy() {
log.Println("onMainWindowDestroy")
}

per eseguire il programma 

go run -tags pango_1_42,gtk_3_22 .


con la libreria nativa il codice e' piu' pulito. L'unico problema e' che si tratta di una libreria dichiarata ancora in alpha e che non risulta aggiornata da due anni

package main

import (
"github.com/andlabs/ui"
)

func main() {
err := ui.Main(func() {
input := ui.NewEntry()
button := ui.NewButton("Ok")
greeting := ui.NewLabel("")
box := ui.NewVerticalBox()
box.Append(ui.NewLabel("Label"), false)
box.Append(input, false)
box.Append(button, false)
box.Append(greeting, false)
window := ui.NewWindow("UI", 200, 100, false)
window.SetMargined(true)
window.SetChild(box)
button.OnClicked(func(*ui.Button) {
greeting.SetText(input.Text())
})
window.OnClosing(func(*ui.Window) bool {
ui.Quit()
return true
})
window.Show()
})
if err != nil {
panic(err)
}
}


martedì 9 luglio 2013

Mandelbrot in Go

Era da un po' di tempo che mi stuzzicava l'idea di provare il linguaggio Go di Google (vedi precedente post) e questa volta ci ho riprovato scrivendo il codice per Mandelbrot da solo senza utilizzare script gia' fatti

Come si puo' vedere ci sono riuscito ma ci sono un paio di considerazioni


al di la' del fatto se Go sia destinato all'oblio (come alcuni progetti di Google) o diventera' lo standard del futuro (personalmente propendo per la prima ipotesi .. ma di solito sbaglio) il linguaggio e' particolarmente ostico per un principiante


  • non si possono eseguire operazioni tra tipi di variabile differenti (pena un errore  mismatched types int float32 invalid operation) a meno di un esplicito cast
  • l'assegnazione di un valore ad una variale mediante il segno "=" e' differente da ":=". In pratica usando := non solo si assegna ma si definisce anche una variabile. Se non si comprende questo meccanisco e si dichiara per esempio var s = "Luca" e subito dopo  s:="Luca" si avra' l'errore variable declared and not used a causa di una doppia dichiarazione della variabile s
  • il linguaggio non e' a formattazione libera e la posizione della parentesi graffa (per esempio) deve essere posizionato in posti precisi pena un errore di compilazione
  • in generale la filosofia del compilatore e': tutti warnings sono errori. Il che puo' essere fastidioso ma mi ha permesso di eliminare una variabile che di fatto non utilizzavo mai (e' la variabile test dei precedenti esempi). In questo senso se si importa una libreria che non viene utilizzata il compilatore genera un errore e non esegue il programma

---------------------------------------------------------------
package main

import (
 "image"
 "image/png"
 "image/color"
 "log"
 "os"
)

func main() {

var SCREEN_WIDTH int = 400
var SCREEN_HEIGHT int =  400

var re_min float32  = -2.0
var im_min float32  = -1.2
var re_max float32  = 1.0
var im_max float32  = 1.2

var iterazioni int = 1024

var a,b float32 
var x,y,x_new,y_new,somma float32
var k,i,j int

var re_factor float32 = (re_max-re_min)
var im_factor float32 = (im_max-im_min)

m := image.NewRGBA(image.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) 

for i=0;i<SCREEN_HEIGHT;i++ {
for j=0;j<SCREEN_WIDTH;j++ {
a = re_min+(float32(j)*re_factor/float32(SCREEN_WIDTH))
      b = im_min+(float32(i)*im_factor/float32(SCREEN_HEIGHT))
x = 0
      y = 0
     
for k=0;k<iterazioni;k++ {
        x_new = (float32(x)*float32(x))-(float32(y)*float32(y))+float32(a)
y_new = (float32(2)*float32(x)*float32(y))+float32(b)
somma = (x_new*x_new)+(y_new*y_new)
        if somma > 4   {
if k%2 == 0 {
m.Set(j, i, color.RGBA{0, 0, 0, 255})
} else {
m.Set(j, i, color.RGBA{255, 255, 255, 255})
}
break
}
        x = x_new;
        y = y_new;
    }
}
}

 f, err := os.OpenFile("mandelbrot.png", os.O_CREATE | os.O_WRONLY, 0666)
 if(err != nil) {
  log.Fatal(err)
  }
 if err = png.Encode(f, m); err != nil {
  log.Fatal(err)
  }
}

venerdì 28 settembre 2012

Primi passi (brutti) con GoLang

Per curiosita' ho voluto provare ad usare il linguaggio Go di Google per creare l'insieme di Mandelbrot e dovendo partire da zero ho cercato un tutorial su Internet andando a cadere qui dove c'e' un esempio completo che riporto



---------------------------------------
import (
 "fmt"
 "os"
 "math"
 "image"
 "image/png"
 "bufio"
 "flag"
)

var pointX = flag.Float64("x", -2.0, "X coordinate of starting point of Mandelbrot or fix point for Julia (range: 2.0 to 2.0)")
var pointY = flag.Float64("y", -2.0, "Y coordinate of starting point of Mandelbrot or fix point for Julia (range: 2.0 to 2.0)")
var zoom = flag.Float64("z", 1.0, "Zoom level (only working properly for Mandelbrot)")
var julia = flag.Bool("julia", false, "Turn on Julia calculation")
var maxIter = flag.Int("maxIter", 51, "Max number of point iterations")
var imgSize = flag.Int("imgSize", 1000, "Size of the image")

func main() {
 flag.Parse()

 fmt.Printf("X: %f\n", *pointX)
 fmt.Printf("Y: %f\n", *pointY)
 fmt.Printf("Zoom: %f\n", *zoom)
 fmt.Printf("Julia: %t\n", *julia)
 fmt.Printf("MaxIter: %d\n", *maxIter)
 fmt.Printf("ImgSize: %d\n", *imgSize)

 start := time.Nanoseconds()
 img := CalculateImage(*imgSize, *imgSize)
 end := time.Nanoseconds()
 fmt.Printf("Time: %d ms\n", (end - start) / 1000 / 1000) // ms
 WriteImage(img)
}

func CalculateImage(imgWidth int, imgHeight int) *image.NRGBA {
 img := image.NewNRGBA(imgWidth, imgHeight)
 minCx := -2.0
 minCy := -2.0
 if !*julia {
 minCx = *pointX
 minCy = *pointY
 }
 maxSquAbs := 4.0 // maximum square of the absolute value
 // calculate step widths
 stepX := math.Abs(minCx - 2.0) / float64(imgWidth) / *zoom
 stepY := math.Abs(minCy - 2.0) / float64(imgHeight) / *zoom
 cx := 0.0
 cy := 0.0
 for px := 0; px < imgWidth; px++ {
 cx = minCx + float64(px) * stepX

 for py := 0; py < imgHeight; py++ {
 cy = minCy + float64(py) * stepY

 iterValue := PointIteration(cx, cy, maxSquAbs, *maxIter)

 color := ChooseColor(iterValue, *maxIter)
 img.Set(px, py, color)
 }
 }
 return img
}

func PointIteration(cx float64, cy float64, maxSquAbs float64, maxIter int) int {
 squAbs := 0.0
 iter := 0
 x := 0.0
 y := 0.0
 if *julia {
 x = cx
 y = cy
 cx = *pointX
 cy = *pointY
 }

 for squAbs <= maxSquAbs && iter < maxIter {
 xt := (x * x) - (y * y) + cx // z^2
 yt := (2.0 * x * y) + cy // z^2
 //xt := x * (x*x - 3*y*y) + cx // z^3
 //yt := y * (3*x*x - y*y) + cy // z^3
 //xt := x * (x*x*x*x - 10*x*x*y*y + 5*y*y*y*y) + cx // z^5
 //yt := y * (5*x*x*x*x - 10*x*x*y*y + y*y*y*y) + cy // z^5
 x = xt
 y = yt
 iter++
 squAbs = (x * x) + (y * y)
 }
 return iter;
}

func ChooseColor(iterValue int, maxIter int) *image.NRGBAColor {
 val := uint8(iterValue)
 if iterValue == maxIter {
 return &image.NRGBAColor {0, 0, 0, 255}
 }
 multi := uint8(255 / maxIter)
 return &image.NRGBAColor {0, val*multi, 0, 255}
 //return &image.NRGBAColor{^(val*multi), ^(val*multi), ^(val*multi), 255} // grey
}

func WriteImage(img *image.NRGBA) {
 file, err := os.Create("mandelbrot.png")
 if err != nil {
 fmt.Printf("Could not create file %s", file.Name())
 }
 writer := bufio.NewWriter(file)
 png.Encode(writer, img)
 writer.Flush()
 file.Close()
}

---------------------------------------

lanciato il comando
go run mandelbrot.go
ho ottenuto i seguenti messaggi di errore

come si vedono ci sono molti errori...possibile che sia stato pubblicato un sorgente cosi' buggato??
andando a vedere meglio nel dettaglio si vede che dalla data di pubblicazione del post (21/1/2012) alla data odierna alcune funzioni sono state sostituite da altre (e non considerate semplicemente come deprecate come di solito accade)

sono abituato ad un linguaggio di programmazione che cambia spesso come accade in Android ma francamente cosi' e' troppo e penso che lascero' stare Go fino a quando non diventa piu' stabile con una migliore documentazione

Tour de France Firenze Gran Depart

  Poagcar Wout Van Aert Vingegaard       Van Der Poel