X-Git-Url: https://git.tokkee.org/?p=go-talk.git;a=blobdiff_plain;f=tex%2Fgo-intro.tex;fp=tex%2Fgo-intro.tex;h=cb532e4dcb3993a41835ff13ff27110a463810b8;hp=0000000000000000000000000000000000000000;hb=05a62c0fec1c93a0905edeeb51d4a77301348af4;hpb=8e533c849e5b0053a1949dcbf98f8948a7501499 diff --git a/tex/go-intro.tex b/tex/go-intro.tex new file mode 100644 index 0000000..cb532e4 --- /dev/null +++ b/tex/go-intro.tex @@ -0,0 +1,493 @@ +% +% Copyright (C) 2015-2016 Sebastian 'tokkee' Harl +% All rights reserved. +% +% Redistribution and use in source and binary forms, with or without +% modification, are permitted provided that the following conditions +% are met: +% 1. Redistributions of source code must retain the above copyright +% notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright +% notice, this list of conditions and the following disclaimer in the +% documentation and/or other materials provided with the distribution. +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +% ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +% OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% +\documentclass[presentation]{beamer} +\uselanguage{German} + +\usetheme{tokkee} +\usefonttheme{professionalfonts} + +\usepackage{xeCJK} +\renewcommand\CJKfamilydefault{\CJKsfdefault} +\setCJKsansfont{WenQuanYi Zen Hei} + +\setbeamercolor*{block body}{fg=black,bg=white} +\setbeamercolor*{block title}{use=titlelike,fg=titlelike.fg!80!black,bg=black!10} + +\usepackage{fontspec,xunicode,polyglossia} +\setdefaultlanguage[spelling=new]{german} + +\usepackage{graphicx} +\usepackage{xcolor} +\definecolor{darkgreen}{rgb}{0,0.39,0} + +\usepackage{listings} +\lstset{ + language=Go, + frame=single, + numbers=left, + keywordstyle=\bfseries\color{darkgreen}, + commentstyle=\itshape\color{blue}, + stringstyle=\color{red}, + showstringspaces=false, + backgroundcolor=\color{lightgray!25}, + literate=% + {Ö}{{\"O}}1 + {Ä}{{\"A}}1 + {Ü}{{\"U}}1 + {ß}{{\ss}}1 + {ü}{{\"u}}1 + {ä}{{\"a}}1 + {ö}{{\"o}}1, +} + +\usepackage[overlay,absolute]{textpos} + +\usepackage{tikz} +\usetikzlibrary{shadows} +\usetikzlibrary{shapes} + +\title[Programmieren mit Go]{Programmieren mit Go} + +\titlegraphic{} + +\copyrightinfo{\copyright{} 2015-2016 Sebastian `tokkee' Harl} + +\author[Sebastian Harl]{Sebastian `tokkee' Harl\\ + $<$sh@tokkee.org$>$} + +\date[2015-2016]{} + +\AtBeginSection[] +{ + \begin{frame} + \begin{center} + \Large + \titlebox{\secname} + \end{center} + \end{frame} +} + +\begin{document} + +\begin{frame}[plain] + \titlepage +\end{frame} + +\begin{frame}{Überblick} + \textbf{Was ist Go?} + + \begin{itemize} + \item \url{https://golang.org/} + \item Open Source Programmiersprache + \item imperativ, Interfaces, Pakete + \item statisch typisiert, kompiliert + \item Nebenläufigkeit + \item Garbage Collection + \end{itemize} + + \begin{tikzpicture}[remember picture,overlay] + \node[anchor=east] at (current page.east) + {\includegraphics[width=.7\textwidth]{gopher}}; + \end{tikzpicture} +\end{frame} + +\begin{frame}[fragile]{Hallo 世界} + \begin{lstlisting} +package main + +import "fmt" + +func main() { + fmt.Println("Hallo 世界") +} + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Programm übersetzen / ausführen} + \begin{lstlisting}[language=bash,numbers=none] +% go build -o hallo hallo.go +% ./hallo +Hallo 世界 + +% go run hallo.go +Hallo 世界 + \end{lstlisting} +\end{frame} + +\begin{frame}{Go Playground} + \begin{center} + \fbox{\includegraphics[width=.9\textwidth]{playground}} + \end{center} +\end{frame} + +\begin{frame}[fragile]{Pakete / Bibliotheken} + \begin{lstlisting} +package zeichenkette + +func Rückwärts(s string) string { + r := []rune(s) + for i := 0; i < len(r)/2; i++ { + j := len(r) - i - 1 + r[j], r[i] = r[i], r[j] + } + return string(r) +} + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Pakete verwenden} + \begin{itemize} + \item \texttt{\$GOPATH/src} ist der Suchpfad für extra Pakete + \item \texttt{import "$<$paketname$>$"} + \end{itemize} + + \begin{lstlisting} +package main + +import ( + "fmt" + + zk "tokkee.org/zeichenkette" +) + +func main() { + fmt.Println(zk.Rückwärts("Hallo 世界")) +} + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Arbeitsumgebung} + \begin{verbatim} +$GOPATH + |\ + | `- bin/ + | \ + | `- hallo + \ + `- src/ + \ + `- hallo/ + \ + `- hallo.go + \end{verbatim} +\end{frame} + +\begin{frame}{Arbeitsumgebung (2)} + \begin{itemize} + \item \lstinline[language=sh]{go build -o hallo_welt hallo} + \item \lstinline[language=sh]{go install hallo} + \item \lstinline[language=sh]{go doc hallo} + \end{itemize} +\end{frame} + +\begin{frame}[fragile]{Fehlerbehandlung} + \begin{lstlisting} +func machEs() (*Object, error) { + var o Object + if err := o.init(); err != nil { + return nil, err + } + return &o, nil +} + +func main() { + o, err := machEs() + if err != nil { + log.Exitf("Fehler: %v\n", err) + } + // ... + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Komplexe Datenstrukturen} + \textbf{Array / Slice} + \begin{lstlisting} +var array [3]string +array[0] = "ein" +array[1] = "String" +fmt.Println(array) + \end{lstlisting} + + \begin{lstlisting} +var slice []string +slice = array[:] +slice = append(slice, "usw", "usf") +slice = []string{"ein", "anderer", "String"} +fmt.Println(slice) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Komplexe Datenstrukturen (2)} + \textbf{Map} + \begin{lstlisting} +var m map[string]int +m = make(map[string]int) // Map anlegen +m = map[string]int{ // Map initialisieren + "a": 1, + "b": 2, +} +m["c"] = 3 +fmt.Println(m["a"]) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Komplexe Datenstrukturen (3)} + \textbf{Struct} + \begin{lstlisting} +type Ding struct { + privat float32 + Öffentlich string +} +d := Ding{ + Öffentlich: "Daten für Alle!", + privat: 47.11, +} +d.privat = 3.1415926535 +fmt.Println(d) + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Komplexe Datenstrukturen (4)} + \textbf{Interface / Methoden} + \begin{lstlisting} +type Macher interface { + Mach(int, int) string +} +type MeinMacher struct {} +func (m MeinMacher) Mach(a, b int) string { + return fmt.Sprintf("%d x %d = %d", a, b, a*b) +} + \end{lstlisting} +\end{frame} + +\begin{frame}{Übung: Quadratwurzel} + \url{https://go-tour-de.appspot.com/flowcontrol/8}\\ + \url{https://de.wikipedia.org/wiki/Newton-Verfahren}\\[5mm] + + Berechne die Quadratwurzel (\lstinline{Wurzel(x float64)}) mit Hilfe des + Newton-Verfahren: + + \begin{equation*} + z_{n+1} = z_n - \frac{z_n^2 - x}{2 * z_n} + \end{equation*} + + Erstelle dazu ein eigenes Paket und ein Programm zum testen. Verwende + entweder eine Schleife mit fester Anzahl an Durchläufen oder bis sich das + Ergebnis nicht (kaum) mehr ändert. +\end{frame} + +\begin{frame}{Die Standard-Bibliothek} + \url{https://golang.org/pkg/}\\[5mm] + + \begin{itemize} + \item Crypto + \item Datenbanken + \item Go Parser + \item Netzwerk, HTTP, SMTP, etc. + \item Datenstrukturen + \end{itemize} +\end{frame} + +\begin{frame}{Weitere Bibliotheken} + \url{https://godoc.org/}\\[5mm] + + \begin{itemize} + \item Vielzahl an Open Source Bibliotheken + \item \lstinline[language=sh]{go get github.com/user/pkg} + \item \url{https://gopkg.in} + \begin{itemize} + \item Versionierung von Bibliotheken auf Github + \item \url{v3} zeigt auf Branch/Tag \url{v3}, \url{v3.N} oder + \url{v3.N.M} + \item \url{gopkg.in/pkg.v3} $\rightarrow$ \url{github.com/go-pkg/pkg} + \item \url{gopkg.in/user/pkg.v3} $\rightarrow$ + \url{github.com/user/pkg} + \end{itemize} + \end{itemize} +\end{frame} + +\begin{frame}[fragile]{Ein Webserver} + \url{https://golang.org/pkg/net/http/} + \begin{lstlisting} +func main() { + http.HandleFunc("/hallo", sageHallo) + log.Fatal(http.ListenAndServe(":9999", nil)) +} + +func sageHallo(w http.ResponseWriter, + r *http.Request) { + + fmt.Fprintf(w, "Hallo %s", r.RemoteAddr) +} + \end{lstlisting} +\end{frame} + +\begin{frame}{Nebenläufigkeit} + \textbf{Alle Anfragen werden nebenläufig behandelt.}\\[5mm] + + \begin{itemize} + \item Goroutinen + \begin{itemize} + \item \texttt{go f(args)} + \item leichtgewichtige Threads + \item hunderte oder tausende werden effizient verwaltet + \end{itemize} + \item Channels + \begin{itemize} + \item Kommunikation zwischen Goroutinen + \end{itemize} + \item \texttt{select} + \begin{itemize} + \item warten auf I/O und Channels + \end{itemize} + \end{itemize} +\end{frame} + +\begin{frame}[fragile]{Goroutinen} + \begin{lstlisting} +errCh := make(chan error, 2) + +go func() { + errCh <- machEs() +}() +go func() { + errCh <- undDas() +}() + +go machEtwasImHintergrund() + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Goroutinen (2)} + \url{https://golang.org/pkg/time/} + \begin{lstlisting} +timeout := time.After(50*time.Millisecond) +for i := 0; i < cap(errCh); i++ { + select { + case err := <- errCh: + if err != nil { + return err + } + case <-timeout: + return fmt.Errorf("timeout(%d)", i) + } +} + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Goroutinen (3)} + \textbf{Synchronisierung}\\ + \url{https://golang.org/pkg/sync/} + \begin{lstlisting} +daten := []Object{...} +var wg sync.WaitGroup +for _, d := range daten { + wg.Add(1) + go func(o Object) { + defer wg.Done() + verarbeite(o) + }(d) // <- !!! +} +wg.Wait() + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Unit Tests} + \url{https://golang.org/pkg/testing/} + \begin{lstlisting} +package zeugs + +// mult multipliziert die beiden +// übergebenen Argumente und gibt +// das Ergebnis zurück. +func mult(a, b int) int { + return a * b +} + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile]{Unit Tests (2)} + \begin{lstlisting} +package zeugs +func TestMult(t *testing.T) { + for _, test := range []struct{ + a, b, want int + }{ + {3, 4, 12}, + {7, 8, 56}, + } { + if r := mult(test.a, test.b); r != test.want { + t.Errorf("mult(%d, %d) = %d; want %d", + test.a, test.b, r, test.want) + } + } +} + \end{lstlisting} +\end{frame} + +\begin{frame}{Übung: Äquivalente Binärbäume} + \url{https://go-tour-de.appspot.com/concurrency/7}\\ + \url{https://godoc.org/golang.org/x/tour/tree}\\ + \url{https://de.wikipedia.org/wiki/Binärbaum}\\[5mm] + + \begin{itemize} + \item Implementiere die \lstinline{Same(t1, t2 *tree.Tree)} Funktion aus + der Online-Übung. + \item Zusätzlich: Erstelle einen Unit-Test für die Funktion. + \end{itemize} +\end{frame} + +\begin{frame}[fragile]{Werkzeuge} + \textbf{Go ist dazu gedacht, in Werkzeugen verwendet zu werden + (\texttt{go/ast}, etc.)}\\[5mm] + + \begin{itemize} + \item gofmt, goimports + \item godoc, \url{https://godoc.org/} + \item IDE und Editor Unterstützung + \end{itemize} + + \textbf{vim:} + \begin{lstlisting}[language=bash,morekeywords={autocmd,let}] +autocmd filetype go + \ autocmd BufWritePre Fmt +let g:gofmt_command = "goimports" + \end{lstlisting} +\end{frame} + +\begin{frame}{\inserttitle} + \begin{center} + Danke für die Aufmerksamkeit\\[2mm] + {\Large Fragen, Kommentare?}\\[6mm] + + \url{https://tour.golang.org}\quad—\quad + \url{https://play.golang.org} + \end{center} +\end{frame} + +\end{document} + +% vim: set shiftwidth=2 softtabstop=2 tabstop=8 noexpandtab spelllang=de_de :