Skip to content
Snippets Groups Projects
Verified Commit 4ca5f5a9 authored by Laurent Modolo's avatar Laurent Modolo
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #505 passed
.Rproj.user
.Rhistory
.RData
.Ruserdata
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/plain-html
pages:
stage: deploy
image: rocker/verse:4.2
script:
- make
- mkdir -p public
- cp presentation.pdf public/
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
LICENSES 0 → 100644
Copyright ©2018 The margo Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the margo project nor the names of its authors and
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
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 HOLDER 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.
all: presentation.pdf
presentation.pdf: presentation.Rmd
Rscript -e "rmarkdown::render('presentation.Rmd')"
[presentation.pdf](https://lbmc.gitbiopages.ens-lyon.fr/hub/formations/go/presentation.pdf)
---
# SPDX-FileCopyrightText: 2022 Laurent Modolo
#
# SPDX-License-Identifier: AGPL-3.0-or-later
title: "Formation Go"
author: "Laurent Modolo"
date: "2022/11/08"
output:
beamer_presentation:
df_print: tibble
fig_caption: no
highlight: tango
latex_engine: xelatex
slide_level: 1
theme: metropolis
ioslides_presentation:
highlight: tango
slidy_presentation:
highlight: tango
classoption: aspectratio=169
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Formation Go:
- ARAMIS
- Sebastien Binet
- Monday 26 September - Tuesday 27 September 2022 All Day
- https://tour.golang.org
\begin{center}
\href{https://github.com/sbinet/margo}{https://github.com/sbinet/margo}
\end{center}
# What is Go?
Go is a new, general-purpose programming language.
- Compiled
- Statically typed
- Concurrent
- Simple
- Productive
History:
- Project starts at Google in 2007 (by Griesemer, Pike, Thompson)
- Open source release in November 2009
# Install Go
[https://go.dev/doc/install](https://go.dev/doc/install)
```sh
mkdir -p $HOME/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
````
everything is in the `go` folder
# Install and run a go programm
```
go install -v github.com/sbinet/margo/cmd/margo-hello@latest
```
go install downloaded (cloned, in git speak) the whole `github.com/sbinet/margo` repository (under `$GOPATH/pkg/mod`) and compiled the `margo-hello` command. As the compilation was successful, it also installed the `margo-hello` command under `$GOPATH/bin`.
# The `go` tool
Compile and run code
```sh
go run hello.go
```
Test code
```sh
go test archive/zip
ok archive/zip 19.557s
```
Build the files in the current directory:
```sh
go build
```
Fetch and install the `websocket` package (in the current module):
```sh
go get golang.org/x/net/websocket
```
# Workspace
```
 go
├──  bin
│ ├──  margo
│ ├──  margo-hello
│ ├──  motion
│ ├──  ...
│ ├──  revive
│ └──  staticcheck
├──  pkg
│ ├──  mod
│ └──  sumdb
└──  src
└──  github.com
```
- `bin` executable binaries
- `pkg` compiled object files
- `src` source code
# `pkg`
```sh
go install -v github.com/sbinet/margo/cmd/margo-hello@latest
```
- `go/pkg/mod/github.com/sbinet/margo@v0.0.0-20220927190215-3215db22d608`
- `go/bin/margo-hello`
# `src`
```sh
mkdir -p $GOPATH/src/gitbio.ens-lyon.fr/margo
cd $GOPATH/src/gitbio.ens-lyon.fr/margo
go mod init
go: creating new go.mod: module gitbio.ens-lyon.fr/margo # create a go.mod file
```
```
mkdir -p $GOPATH/src/gitbio.ens-lyon.fr/margo/hello
cp $GOPATH/pkg/mod/github.com/sbinet/margo@v0.0.0-20220927190215-3215db22d608/cmd/margo-hello/main.go $GOPATH/src/gitbio.ens-lyon.fr/margo/hello/
```
```
go install gitbio.ens-lyon.fr/margo/hello
hello
Hello MarGo!
```
# Langage
- primitive types
```go
int, uint, uint8, ..., uint64, int8, ... int64
bool, string
float32, float64
complex64, complex128
```
- structs
```go
struct {
Name string
Year int
}
```
# Langage
- arrays and slices
```go
[3]string, []float64
```
- maps
```go
map[string]int
```
- pointers
```go
*int, *Person
```
# Declaration
Normal declaration:
```go
var text string = "hello"
```
You can omit types:
```go
var text = "hello"
```
```go
text := "hello" //create and init
```
This code won't compile:
```
var freezing Fahrenheit = 32
var boiling Celsius = 100
sauna := (freezing + boiling) / 2
```
There's no implicit numeric conversion in Go.
# Functions
A sum function:
```go
func sum(a int, b int) int {
return a + b
}
```
A function with multiple return values:
Made clearer by naming the return values:
```go
func divMod(a, b int) (quo, rem int) {
return a / b, a % b
}
```
# Pointers
Go has pointers:
```go
var p *int
p = new(int)
```
But no pointer arithmetics:
```go
var p *int = &a[0]
var q = p+2 // invalid
```
There's `new` but there's no `delete`.
Memory is garbaged collected after it's no longer accessible.
# Memory allocation
The compiler decides where to allocate based on escape analysis.
Using `new` doesn't imply using the heap:
`stack.go`:
```go
func get() int {
n := new(int)
return *n
}
````
And not all values in the heap are created with `new`:
`heap.go`:
```go
func get() *int {
n := 4
return &n
}
```
# Packages & Imports
Every [[https://golang.org/][Go]] program is made up of packages.
Programs start running in `package` `main`.
This program is using the packages with import paths `"fmt"` and `"math/rand"`.
By convention, the package name is the same as the last element of the import path.
For instance, the `"math/rand"` package comprises files that begin with the statement `package` `rand`.
```
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
```
# Exported names
In [[https://golang.org/][Go]], a name is *exported* if it begins with a *capital* *letter*.
For example, `Pizza` is an exported name, as is `Pi`, which is exported from the `math` package.
`pizza` and `pi` do not start with a capital letter, so they are not exported.
When importing a package, you can refer only to its exported names.
Any "unexported" names are not accessible from outside the package.
Run the code. Notice the error message.
# Documentation
Browsing documentation is performed with `go` `doc`.
```sh
go doc os | less
PACKAGE DOCUMENTATION
package os
import "os"
Package os provides a platform-independent interface to operating system
functionality. The design is Unix-like, although the error handling is
Go-like; failing calls return values of type error rather than error
numbers. Often, more information is available within the error. For
example, if a call that takes a file name fails, such as Open or Stat,
the error will include the failing file name when printed and will be of
type *PathError, which may be unpacked for more information.
```
# Documentation
Browsing documentation is performed with `go` `doc`.
```sh
go doc os File | less
type File struct {
// contains filtered or unexported fields
}
File represents an open file descriptor.
func Create(name string) (file *File, err error)
Create creates the named file mode 0666 (before umask), truncating it if
it already exists. If successful, methods on the returned File can be
used for I/O; the associated file descriptor has mode O_RDWR. If there
is an error, it will be of type *PathError.
func Open(name string) (file *File, err error)
Open opens the named file for reading. If successful, methods on the
returned file can be used for reading; the associated file descriptor
has mode O_RDONLY. If there is an error, it will be of type *PathError.
```
# Goroutines
What is a goroutine? It's an independently executing function, launched by a go statement.
It has its own call stack, which grows and shrinks as required.
It's very cheap. It's practical to have thousands, even hundreds of thousands of goroutines.
It's not a thread.
There might be only one thread in a program with thousands of goroutines.
Instead, goroutines are multiplexed dynamically onto threads as needed to keep all the goroutines running.
But if you think of it as a very cheap thread, you won't be far off
# Goroutines
```go
func main() {
boring("boring!")
}
func boring(msg string) {
for i := 0; ; i++ {
fmt.Println(msg, i)
time.Sleep(time.Second)
}
}
```
# Channels
A channel in Go provides a connection between two goroutines, allowing them to communicate.
```go
func main() {
c := make(chan string)
go boring("boring!", c)
for i := 0; i < 5; i++ {
fmt.Printf("You say: %q\n", <-c) // Receive expression is just a value.
}
}
```
# Channels
A channel in Go provides a connection between two goroutines, allowing them to communicate.
```go
func boring(msg string, c chan string) {
for i := 0; ; i++ {
c <- fmt.Sprintf("%s %d", msg, i) // Expression to be sent can be any suitable value.
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}
```
# For scientific development
https://github.com/gonum
\begin{center}
\href{https://doc.rust-lang.org/book/}{https://doc.rust-lang.org/book/}
\end{center}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment