14
C-spirit reborn, or why Go was bound to be created BOHDAN TROTSENKO

C-spirit reborn: why Go was bound to be created

Embed Size (px)

DESCRIPTION

Сhannels — почему это круто для мультизадачности, defer и другие особенности Go.

Citation preview

Page 1: C-spirit reborn: why Go was bound to be created

C-spirit reborn, or why Go was bound to be createdBOHDAN TROTSENKO

Page 2: C-spirit reborn: why Go was bound to be created

C, C++ CMyClass instance;

instance.DoSomething();

Page 3: C-spirit reborn: why Go was bound to be created

… void *p = get_memory_on_stack(sizeof(CMyClass));

try {

p.ctr();

p.DoSomething();

} catch (...)

{

p.~dstr();

}

Page 4: C-spirit reborn: why Go was bound to be created

Imperative Languages

C, C++

Go

Java, C#

Page 5: C-spirit reborn: why Go was bound to be created

Enterprise principle

The straight-forward way to do things is the right way to do things

Page 6: C-spirit reborn: why Go was bound to be created

func FillInp(path string, res chan string) {

var (

file *os.File

err error

)

defer close(res)

if file, err = os.Open(path); err != nil {

fmt.Println("Error opening file", err)

return

}

defer file.Close()

Page 7: C-spirit reborn: why Go was bound to be created

… some horrible code sem := make(chan bool, parallelism) // semaphore

for perm := 0; perm < parallelism; perm++ {

sem <- true // add initial values

}

for cs := 0; cs < len(cases); cs++ {

<-sem // read values

thiscase := cs

go func() {

Solve(&cases[thiscase])

sem <- true // permit others to continue

}()

}

Page 8: C-spirit reborn: why Go was bound to be created
Page 9: C-spirit reborn: why Go was bound to be created
Page 10: C-spirit reborn: why Go was bound to be created

protobuf message Test {

required string label = 1;

optional int32 type = 2 [default=77];

repeated int64 reps = 3;

optional group OptionalGroup = 4 {

required string RequiredField = 5;

}

}

Page 11: C-spirit reborn: why Go was bound to be created

usage of protobuf test := &example.Test {

Label: proto.String("hello"),

Type: proto.Int32(17),

Optionalgroup: &example.Test_OptionalGroup {

RequiredField: proto.String("good bye"),

},

}

data, err := proto.Marshal(test)

if err != nil {

log.Fatal("marshaling error: ", err)

}

Page 12: C-spirit reborn: why Go was bound to be created

A few gotchas… dbg_f("Awaiting writer...")

<-outWG

dbg_ln("done.")

////////////////

hostname := packet[start:end]

Page 13: C-spirit reborn: why Go was bound to be created

Bottom line Go is lightweight and powerfull

Collections and memory made right

Concurrency made right

Software architecture made wonderful

Page 14: C-spirit reborn: why Go was bound to be created

[email protected]

http://trotsenko.com.ua

@modosansreves