1 %
2 % Copyright (C) 2015-2016 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 % All rights reserved.
4 %
5 % Redistribution and use in source and binary forms, with or without
6 % modification, are permitted provided that the following conditions
7 % are met:
8 % 1. Redistributions of source code must retain the above copyright
9 % notice, this list of conditions and the following disclaimer.
10 % 2. Redistributions in binary form must reproduce the above copyright
11 % notice, this list of conditions and the following disclaimer in the
12 % documentation and/or other materials provided with the distribution.
13 %
14 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 % ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 % TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 % CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 % EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 % PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 % OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 % WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 % OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 % ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 %
26 \documentclass[presentation]{beamer}
27 \uselanguage{German}
29 \usetheme{tokkee}
30 \usefonttheme{professionalfonts}
32 \usepackage{xeCJK}
33 \renewcommand\CJKfamilydefault{\CJKsfdefault}
34 \setCJKsansfont{WenQuanYi Zen Hei}
36 \setbeamercolor*{block body}{fg=black,bg=white}
37 \setbeamercolor*{block title}{use=titlelike,fg=titlelike.fg!80!black,bg=black!10}
39 \usepackage{fontspec,xunicode,polyglossia}
40 \setdefaultlanguage[spelling=new]{german}
42 \usepackage{graphicx}
43 \usepackage{xcolor}
44 \definecolor{darkgreen}{rgb}{0,0.39,0}
46 \usepackage{listings}
47 \lstset{
48 language=Go,
49 frame=single,
50 numbers=left,
51 keywordstyle=\bfseries\color{darkgreen},
52 commentstyle=\itshape\color{blue},
53 stringstyle=\color{red},
54 showstringspaces=false,
55 backgroundcolor=\color{lightgray!25},
56 literate=%
57 {Ö}{{\"O}}1
58 {Ä}{{\"A}}1
59 {Ü}{{\"U}}1
60 {ß}{{\ss}}1
61 {ü}{{\"u}}1
62 {ä}{{\"a}}1
63 {ö}{{\"o}}1,
64 }
66 \usepackage[overlay,absolute]{textpos}
68 \usepackage{tikz}
69 \usetikzlibrary{shadows}
70 \usetikzlibrary{shapes}
72 \title[Programmieren mit Go]{Programmieren mit Go}
74 \titlegraphic{}
76 \copyrightinfo{\copyright{} 2015-2016 Sebastian `tokkee' Harl}
78 \author[Sebastian Harl]{Sebastian `tokkee' Harl\\
79 $<$sh@tokkee.org$>$}
81 \date[2015-2016]{}
83 \AtBeginSection[]
84 {
85 \begin{frame}
86 \begin{center}
87 \Large
88 \titlebox{\secname}
89 \end{center}
90 \end{frame}
91 }
93 \begin{document}
95 \begin{frame}[plain]
96 \titlepage
97 \end{frame}
99 \begin{frame}{Überblick}
100 \textbf{Was ist Go?}
102 \begin{itemize}
103 \item \url{https://golang.org/}
104 \item Open Source Programmiersprache
105 \item imperativ, Interfaces, Pakete
106 \item statisch typisiert, kompiliert
107 \item Nebenläufigkeit
108 \item Garbage Collection
109 \end{itemize}
111 \begin{tikzpicture}[remember picture,overlay]
112 \node[anchor=east] at (current page.east)
113 {\includegraphics[width=.7\textwidth]{gopher}};
114 \end{tikzpicture}
115 \end{frame}
117 \begin{frame}[fragile]{Hallo 世界}
118 \begin{lstlisting}
119 package main
121 import "fmt"
123 func main() {
124 fmt.Println("Hallo 世界")
125 }
126 \end{lstlisting}
127 \end{frame}
129 \begin{frame}[fragile]{Programm übersetzen / ausführen}
130 \begin{lstlisting}[language=bash,numbers=none]
131 % go build -o hallo hallo.go
132 % ./hallo
133 Hallo 世界
135 % go run hallo.go
136 Hallo 世界
137 \end{lstlisting}
138 \end{frame}
140 \begin{frame}{Go Playground}
141 \begin{center}
142 \fbox{\includegraphics[width=.9\textwidth]{playground}}
143 \end{center}
144 \end{frame}
146 \begin{frame}[fragile]{Pakete / Bibliotheken}
147 \begin{lstlisting}
148 package zeichenkette
150 func Rückwärts(s string) string {
151 r := []rune(s)
152 for i := 0; i < len(r)/2; i++ {
153 j := len(r) - i - 1
154 r[j], r[i] = r[i], r[j]
155 }
156 return string(r)
157 }
158 \end{lstlisting}
159 \end{frame}
161 \begin{frame}[fragile]{Pakete verwenden}
162 \begin{itemize}
163 \item \texttt{\$GOPATH/src} ist der Suchpfad für extra Pakete
164 \item \texttt{import "$<$paketname$>$"}
165 \end{itemize}
167 \begin{lstlisting}
168 package main
170 import (
171 "fmt"
173 zk "tokkee.org/zeichenkette"
174 )
176 func main() {
177 fmt.Println(zk.Rückwärts("Hallo 世界"))
178 }
179 \end{lstlisting}
180 \end{frame}
182 \begin{frame}[fragile]{Arbeitsumgebung}
183 \begin{verbatim}
184 $GOPATH
185 |\
186 | `- bin/
187 | \
188 | `- hallo
189 \
190 `- src/
191 \
192 `- hallo/
193 \
194 `- hallo.go
195 \end{verbatim}
196 \end{frame}
198 \begin{frame}{Arbeitsumgebung (2)}
199 \begin{itemize}
200 \item \lstinline[language=sh]{go build -o hallo_welt hallo}
201 \item \lstinline[language=sh]{go install hallo}
202 \item \lstinline[language=sh]{go doc hallo}
203 \end{itemize}
204 \end{frame}
206 \begin{frame}[fragile]{Fehlerbehandlung}
207 \begin{lstlisting}
208 func machEs() (*Object, error) {
209 var o Object
210 if err := o.init(); err != nil {
211 return nil, err
212 }
213 return &o, nil
214 }
216 func main() {
217 o, err := machEs()
218 if err != nil {
219 log.Exitf("Fehler: %v\n", err)
220 }
221 // ...
222 \end{lstlisting}
223 \end{frame}
225 \begin{frame}[fragile]{Komplexe Datenstrukturen}
226 \textbf{Array / Slice}
227 \begin{lstlisting}
228 var array [3]string
229 array[0] = "ein"
230 array[1] = "String"
231 fmt.Println(array)
232 \end{lstlisting}
234 \begin{lstlisting}
235 var slice []string
236 slice = array[:]
237 slice = append(slice, "usw", "usf")
238 slice = []string{"ein", "anderer", "String"}
239 fmt.Println(slice)
240 \end{lstlisting}
241 \end{frame}
243 \begin{frame}[fragile]{Komplexe Datenstrukturen (2)}
244 \textbf{Map}
245 \begin{lstlisting}
246 var m map[string]int
247 m = make(map[string]int) // Map anlegen
248 m = map[string]int{ // Map initialisieren
249 "a": 1,
250 "b": 2,
251 }
252 m["c"] = 3
253 fmt.Println(m["a"])
254 \end{lstlisting}
255 \end{frame}
257 \begin{frame}[fragile]{Komplexe Datenstrukturen (3)}
258 \textbf{Struct}
259 \begin{lstlisting}
260 type Ding struct {
261 privat float32
262 Öffentlich string
263 }
264 d := Ding{
265 Öffentlich: "Daten für Alle!",
266 privat: 47.11,
267 }
268 d.privat = 3.1415926535
269 fmt.Println(d)
270 \end{lstlisting}
271 \end{frame}
273 \begin{frame}[fragile]{Komplexe Datenstrukturen (4)}
274 \textbf{Interface / Methoden}
275 \begin{lstlisting}
276 type Macher interface {
277 Mach(int, int) string
278 }
279 type MeinMacher struct {}
280 func (m MeinMacher) Mach(a, b int) string {
281 return fmt.Sprintf("%d x %d = %d", a, b, a*b)
282 }
283 \end{lstlisting}
284 \end{frame}
286 \begin{frame}{Übung: Quadratwurzel}
287 \url{https://go-tour-de.appspot.com/flowcontrol/8}\\
288 \url{https://de.wikipedia.org/wiki/Newton-Verfahren}\\[5mm]
290 Berechne die Quadratwurzel (\lstinline{Wurzel(x float64)}) mit Hilfe des
291 Newton-Verfahren:
293 \begin{equation*}
294 z_{n+1} = z_n - \frac{z_n^2 - x}{2 * z_n}
295 \end{equation*}
297 Erstelle dazu ein eigenes Paket und ein Programm zum testen. Verwende
298 entweder eine Schleife mit fester Anzahl an Durchläufen oder bis sich das
299 Ergebnis nicht (kaum) mehr ändert.
300 \end{frame}
302 \begin{frame}{Die Standard-Bibliothek}
303 \url{https://golang.org/pkg/}\\[5mm]
305 \begin{itemize}
306 \item Crypto
307 \item Datenbanken
308 \item Go Parser
309 \item Netzwerk, HTTP, SMTP, etc.
310 \item Datenstrukturen
311 \end{itemize}
312 \end{frame}
314 \begin{frame}{Weitere Bibliotheken}
315 \url{https://godoc.org/}\\[5mm]
317 \begin{itemize}
318 \item Vielzahl an Open Source Bibliotheken
319 \item \lstinline[language=sh]{go get github.com/user/pkg}
320 \item \url{https://gopkg.in}
321 \begin{itemize}
322 \item Versionierung von Bibliotheken auf Github
323 \item \url{v3} zeigt auf Branch/Tag \url{v3}, \url{v3.N} oder
324 \url{v3.N.M}
325 \item \url{gopkg.in/pkg.v3} $\rightarrow$ \url{github.com/go-pkg/pkg}
326 \item \url{gopkg.in/user/pkg.v3} $\rightarrow$
327 \url{github.com/user/pkg}
328 \end{itemize}
329 \end{itemize}
330 \end{frame}
332 \begin{frame}[fragile]{Ein Webserver}
333 \url{https://golang.org/pkg/net/http/}
334 \begin{lstlisting}
335 func main() {
336 http.HandleFunc("/hallo", sageHallo)
337 log.Fatal(http.ListenAndServe(":9999", nil))
338 }
340 func sageHallo(w http.ResponseWriter,
341 r *http.Request) {
343 fmt.Fprintf(w, "Hallo %s", r.RemoteAddr)
344 }
345 \end{lstlisting}
346 \end{frame}
348 \begin{frame}{Nebenläufigkeit}
349 \textbf{Alle Anfragen werden nebenläufig behandelt.}\\[5mm]
351 \begin{itemize}
352 \item Goroutinen
353 \begin{itemize}
354 \item \texttt{go f(args)}
355 \item leichtgewichtige Threads
356 \item hunderte oder tausende werden effizient verwaltet
357 \end{itemize}
358 \item Channels
359 \begin{itemize}
360 \item Kommunikation zwischen Goroutinen
361 \end{itemize}
362 \item \texttt{select}
363 \begin{itemize}
364 \item warten auf I/O und Channels
365 \end{itemize}
366 \end{itemize}
367 \end{frame}
369 \begin{frame}[fragile]{Goroutinen}
370 \begin{lstlisting}
371 errCh := make(chan error, 2)
373 go func() {
374 errCh <- machEs()
375 }()
376 go func() {
377 errCh <- undDas()
378 }()
380 go machEtwasImHintergrund()
381 \end{lstlisting}
382 \end{frame}
384 \begin{frame}[fragile]{Goroutinen (2)}
385 \url{https://golang.org/pkg/time/}
386 \begin{lstlisting}
387 timeout := time.After(50*time.Millisecond)
388 for i := 0; i < cap(errCh); i++ {
389 select {
390 case err := <- errCh:
391 if err != nil {
392 return err
393 }
394 case <-timeout:
395 return fmt.Errorf("timeout(%d)", i)
396 }
397 }
398 \end{lstlisting}
399 \end{frame}
401 \begin{frame}[fragile]{Goroutinen (3)}
402 \textbf{Synchronisierung}\\
403 \url{https://golang.org/pkg/sync/}
404 \begin{lstlisting}
405 daten := []Object{...}
406 var wg sync.WaitGroup
407 for _, d := range daten {
408 wg.Add(1)
409 go func(o Object) {
410 defer wg.Done()
411 verarbeite(o)
412 }(d) // <- !!!
413 }
414 wg.Wait()
415 \end{lstlisting}
416 \end{frame}
418 \begin{frame}[fragile]{Unit Tests}
419 \url{https://golang.org/pkg/testing/}
420 \begin{lstlisting}
421 package zeugs
423 // mult multipliziert die beiden
424 // übergebenen Argumente und gibt
425 // das Ergebnis zurück.
426 func mult(a, b int) int {
427 return a * b
428 }
429 \end{lstlisting}
430 \end{frame}
432 \begin{frame}[fragile]{Unit Tests (2)}
433 \begin{lstlisting}
434 package zeugs
435 func TestMult(t *testing.T) {
436 for _, test := range []struct{
437 a, b, want int
438 }{
439 {3, 4, 12},
440 {7, 8, 56},
441 } {
442 if r := mult(test.a, test.b); r != test.want {
443 t.Errorf("mult(%d, %d) = %d; want %d",
444 test.a, test.b, r, test.want)
445 }
446 }
447 }
448 \end{lstlisting}
449 \end{frame}
451 \begin{frame}{Übung: Äquivalente Binärbäume}
452 \url{https://go-tour-de.appspot.com/concurrency/7}\\
453 \url{https://godoc.org/golang.org/x/tour/tree}\\
454 \url{https://de.wikipedia.org/wiki/Binärbaum}\\[5mm]
456 \begin{itemize}
457 \item Implementiere die \lstinline{Same(t1, t2 *tree.Tree)} Funktion aus
458 der Online-Übung.
459 \item Zusätzlich: Erstelle einen Unit-Test für die Funktion.
460 \end{itemize}
461 \end{frame}
463 \begin{frame}[fragile]{Werkzeuge}
464 \textbf{Go ist dazu gedacht, in Werkzeugen verwendet zu werden
465 (\texttt{go/ast}, etc.)}\\[5mm]
467 \begin{itemize}
468 \item gofmt, goimports
469 \item godoc, \url{https://godoc.org/}
470 \item IDE und Editor Unterstützung
471 \end{itemize}
473 \textbf{vim:}
474 \begin{lstlisting}[language=bash,morekeywords={autocmd,let}]
475 autocmd filetype go
476 \ autocmd BufWritePre <buffer> Fmt
477 let g:gofmt_command = "goimports"
478 \end{lstlisting}
479 \end{frame}
481 \begin{frame}{\inserttitle}
482 \begin{center}
483 Danke für die Aufmerksamkeit\\[2mm]
484 {\Large Fragen, Kommentare?}\\[6mm]
486 \url{https://tour.golang.org}\quad—\quad
487 \url{https://play.golang.org}
488 \end{center}
489 \end{frame}
491 \end{document}
493 % vim: set shiftwidth=2 softtabstop=2 tabstop=8 noexpandtab spelllang=de_de :