Java to Go Google Go für Java Entwickler

467.882 Zeilen Code

2.130 Contributors

3 Gründe für Go

  1. Einfach
  2. Mächtig
  3. Langweilig

Hello Gopher


                        package main
    
                        import "fmt"
                        
                        func main() {
                          fmt.Println("Hello Gopher!")
                        }
                    
Ausführen

                        go build hellogopher.go // 1. Code kompilieren
                        ./hellogopher           // 2. Binary ausführen
                        

                            go run hellogopher.go   // Code kompilieren und ausführen
                        

Entwicklung

JetBrains GoLand
Visual Studio Code
Vim Go

5 Fakten zu Go

  1. statisches Typsystem
  2. Garbage Collection
  3. keine Vererbung
  4. Concurrency eingebaut
  5. native Ausführung
    Linux, Win, z/OS, 386, amd64, ARM, wasm, ...

Variablen, Slices, Schleifen


                          // Variable
                          var frank string = "Frank"
                          claire := "Claire"
       
                          // Array (fixe Länge)
                          namesArray := [3]string{frank, claire, "Zoe"}
    
                          // Slice (variable Länge)
                          namesSlice := make([]string, 2)
                          namesSlice[0] = frank
     
                          // Schleife
                          for i, name := range namesSlice {
                            fmt.Println("Hello " + name + "!")
                          }
                   

Struct statt Klasse


                        type Congressman struct {
                          Name string
                        }
                        
                        func main() {
                          c := Congressman{Name: "Peter Russo"}
                          fmt.Println("Hello " + c.Name + "!")
                        }
                 

Function Receiver statt Instanzmethode


                        type Congressman struct {
                          Name string
                        }
                        
                        func (c Congressman) swearOathOfOffice() {
                          fmt.Printf("I, %v, swear to serve the USA.", c.Name)
                        }
                        
                        func main() {
                          c := Congressman{Name: "Peter Russo"}
                          c.swearOathOfOffice();
                        }
                    

Interface


                            type Greeter interface {
                              greet()   
                            }
        
                            func passBy(c1 Greeter, c2 Greeter) {
                              c1.greet()
                              c2.greet()  
                            }
                            
                            func main() {
                              c := Congressman{Name: "Frank U."}
                              e := Enemy{}
                              passBy(c, e)
                            }
                        

                    type Congressman struct {
                        Name string
                    }

                    func (c Congressman) greet() {
                      fmt.Println("Hello", c.Name)
                    }
                        

                            type Enemy struct{}
         
                            func (e Enemy) greet() {
                             fmt.Println("Go to hell!")   
                            }
                        

Zu Einfach?

Struct Embedding statt Vererbung


                        type Congressman struct {
                          Name string
                        }

                        type President struct {
                          Congressman  // Embedded

                          NuclearWeaponCode string
                        }
                          
                        func main() {
                          p := President{NuclearWeaponCode: "123"}
                          p.Name = "Frank Underwood"
                          p.swearOathOfOffice();
                        }
                    

Fehler


                                // Fehler als Rückgabewert
                                func (c Congressman) bribe(amount float64) error {
                                  if c.Name != "Peter Russo" {
                                      return errors.New("Not corrupt!")
                                  }
                                  c.AccountBalance += amount
                                  return nil
                                }
    
                                func main() {
                                  c := Congressman{Name: "Jackie Sharp", AccountBalance: -10.0}

                                  // Fehler behandeln
                                  err := c.bribe(5000.0)
                                  if err != nil {
                                      fmt.Printf("%v is not bribeable.", c.Name)
                                  }
                                }
                            

Generics


                                func printSliceOfInts(numbers []int) {
                                    for _, num := range numbers {
                                        fmt.Print(num, " ")
                                    }
                                }
                        

                            func printSliceOfStrings(strings []string) {
                                for _, num := range strings {
                                    fmt.Print(num, " ")
                                }
                            }
                        
Kommen in Go 1.18 im Februar 2022!

Mächtig

Concurrency

Goroutine

leichtgewichtiger Thread

    Channel

    Kanal für Nachrichten, dient der

    Kommunkation

    Synchronisation

    von Goroutinen

    Goroutine

    
                            func HelloCongressman(name string) {
                              fmt.Println("Hello Congressman", name)
                            }
                        
                            func main() {
                              go HelloCongressman("Russo")
                            }
                        

    Channel

                            
                            // Deklarieren und Initialisieren
                            c := make(chan int)
    
                            // Nachricht über Channel senden
                            c <- 1
    
                            // Nachricht von Channel empfangen,
                            // "Pfeil" gibt Richtung des Datenfluss an
                            value = <-c
                          

    Debatte mit Goroutinen und Channels

                            
                            func speaker(name string, debate chan int) {
                              for {
                                microphone := <-debate // Auf Mikro warten (Nachricht empfangen)
                            
                                fmt.Printf("Turn %v: %v says '%v'\n", microphone, name, randomAnswer())
                                time.Sleep(400 * time.Millisecond)
                            
                                microphone++
                                debate <- microphone // Mikro zurückgeben (Nachricht senden)
                              }
                            }
                            
                            func main() {
                              debate := make(chan int) // Channel deklarieren und initialisieren
                            
                              go speaker("Jackie", debate)
                              go speaker("Frank", debate)
                            
                              microphone := 1
                              debate <- microphone        // Mikro geben und Diskussion starten
                              time.Sleep(2 * time.Second) // Dauer der Diskussion
                              <-debate                    // Mikro nehmen und Diskussion beenden
                            }
                        

    Debatte mit Goroutinen und Channels

                            
                            >_ go run debate.go
                            Turn 1: Frank says 'You liar.'
                            Turn 2: Jackie says 'Back off.'
                            Turn 3: Frank says 'Back off.'
                            Turn 4: Jackie says 'My point.'
                            Turn 5: Frank says 'You liar.'
                            Turn 6: Jackie says 'You're wrong.'
                        

    Standardbibliothek

    • Tests
    • HTTP(2) Server und Router
    • JSON
    • Logging

    Einfach Langweilig

    • Variablen, Slices, Schleifen
    • Struct

     
    Zu Einfach?

    • Struct Embedding
    • Fehler
    • Generics

     
    Mächtig

    • Interface
    • Goroutine
    • Channel
    • Standardbibliothek

    Demo

    Go Liebt

    Microservices
    Serverless Functions
    Kommandozeilen-Tools
    DevOps und Cloud