Large Scale Programming with Modules

Concepts of Programming Languages

Sebastian Macke

Rosenheim Technical University

Enterprise Programming

2

What is a Module?

The major advancement in the area of modular programming has been the
development of coding techniques and assemblers which
  (1) allow one module to be written with little knowledge of the code in another module, and
  (2) allow modules to be reassembled and replaced without reassembly of the whole system.
3

History

4

Today

5

Visibility / Information Hiding Principle

6

Distribution and Replaceability

7

Versioning

8

Versions and Conflicts

9

Module System in Java

10

Modules in other programming languages

11

The Go Module System

12

The Go Module System

13

The Principles of Versioning in Go

Repeatability
Make sure that build results never change over time !!!

Compatibility
Make sure that released code stays compatible. Make incompatible changes explicit visible to users of your code !!!

Cooperation
The package ecosystem is the most important factor for Go’s success.
Make sure your code ist stable with the latest minor version of a dependent library

14

Semantic Versioning

Change ...

15

The go.mod File

Go module: a tree (directory) of source files with a go.mod file in the root directory

Example go.mod file:

module github.com/my/module/v3

go 1.14

require (
    github.com/some/dependency v1.2.3
    github.com/another/dependency v0.1.0
    github.com/additional/dependency/v4 v4.0.0
)
16

How to create a new module

A module can be created with a simple command:

mkdir module
cd module
go mod init example.com/my/module

If your code is already under version control (e.g. git), you may simply run

go mod init
17

Demo

Use my own repo as module

import (
    "fmt"
    "github.com/s-macke/concepts-of-programming-languages/src/oop/stack"
)

func main() {
    s := stack.NewStack()
    s.Push(1)
    fmt.Println(s.Pop())
}
18

Managing Dependencies

require (
    github.com/some/dependency v1.2.3
    github.com/another/dependency v0.1.0
    github.com/additional/dependency/v4 v4.0.0
)
19

Exclude and Replace Modules

exclude (
    golang.org/x/crypto v1.4.5
    golang.org/x/text v1.6.7
)
replace (
    golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
    golang.org/x/net => example.com/fork/net v1.4.5
    golang.org/x/net v1.2.3 => ../fork/net
    golang.org/x/net => ../fork/net
)
20

Minimal Version Selection

21

The go.sum File

github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
22

Module Downloads

23

Hands On

24

Plugins

25

Golang and Libraries

26

Linking of C libraries

#include <stdio.h>
void myprint(char *s);
#include "mylib.h"

void myprint(char *s) {
    printf("You said: %s\n", s);
}
/*
int main() {
    return 0;
}
*/
package main

// #cgo CFLAGS: -I.
// // #cgo LDFLAGS: libmylib.a  // for static linking
// #cgo LDFLAGS: -L. -lmylib    // for shared object linking
// #include "mylib.h"
// #include <stdlib.h>
import "C"
import "unsafe"

func main() {
    s := C.CString("Hello Lib!")
    defer C.free(unsafe.Pointer(s))
    C.myprint(s)
}
27

Dynamic linking of C libraries

dynamic:
    # compile the source into an object file (position independent code)
    gcc -fPIC -c mylib.c
    # convert the resulting object file into a shared library
    gcc -shared -o libmylib.so mylib.o

Deleting the libmylib.so throws error:

./main
dyld: Library not loaded: libmylib.so
  Referenced from: ./main
  Reason: image not found
zsh: abort      ./main
28

Static linking of C libraries

static:
    # compile the source into an object file
    gcc -c mylib.c
    # convert the resulting object file into a library
    ar rc libmylib.a mylib.o
    # build an index inside the library
    ranlib libmylib.a

Deleting the libmylib.a is fine:

./main
You said: Hello Lib!
29

Plugins

ELF = Executable and Linkable Format: common standard file format for shared libraries

30

Golang Plugin Example

Plugins can be opened using Open() provided in package plugin:

p, err := plugin.Open(os.Args[1])

Lookup searches for a symbol named MyFunc in plugin p. A Symbol is any exported variable or function.

Looking up a function:

// Code in plugin: func MyFunc() { return "Hello!" }
myFuncSymbol, err := p.Lookup("MyFunc")
var myFunc func() := myFuncSymbol.(func())
myFunc()

Looking up a variable:

// Code in plugin: var MyVar int
myVarSymbol, err := p.Lookup("MyVar")
var myVar *int = v.(*int)
*myVar = 42
31

Golang Plugins Caveats

32

Golang Plugin Examples

33

Hands On

Windows users: use Golang in your Windows Subsystem for Linux or a Docker container.

34

Thank you

Sebastian Macke

Rosenheim Technical University

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)