Code for ch01 + ch02
This commit is contained in:
parent
b9f003bb57
commit
4e366cd04a
32 changed files with 618 additions and 0 deletions
9
ch01/aSourceFile.go
Normal file
9
ch01/aSourceFile.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("This is a sample Go program!")
|
||||
}
|
||||
31
ch01/cla.go
Normal file
31
ch01/cla.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
fmt.Println("Please give one or more floats.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
arguments := os.Args
|
||||
min, _ := strconv.ParseFloat(arguments[1], 64)
|
||||
max, _ := strconv.ParseFloat(arguments[1], 64)
|
||||
|
||||
for i := 2; i < len(arguments); i++ {
|
||||
n, _ := strconv.ParseFloat(arguments[i], 64)
|
||||
if n < min {
|
||||
min = n
|
||||
}
|
||||
if n > max {
|
||||
max = n
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Min:", min)
|
||||
fmt.Println("Max:", max)
|
||||
}
|
||||
11
ch01/curly.go
Normal file
11
ch01/curly.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main()
|
||||
{
|
||||
fmt.Println("Go has strict rules for curly braces!")
|
||||
}
|
||||
|
||||
44
ch01/errors.go
Normal file
44
ch01/errors.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
fmt.Println("Please give one or more floats.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
arguments := os.Args
|
||||
var err error = errors.New("An error")
|
||||
k := 1
|
||||
var n float64
|
||||
for err != nil {
|
||||
if k >= len(arguments) {
|
||||
fmt.Println("None of the arguments is a float!")
|
||||
return
|
||||
}
|
||||
n, err = strconv.ParseFloat(arguments[k], 64)
|
||||
k++
|
||||
}
|
||||
|
||||
min, max := n, n
|
||||
for i := 2; i < len(arguments); i++ {
|
||||
n, err := strconv.ParseFloat(arguments[i], 64)
|
||||
if err == nil {
|
||||
if n < min {
|
||||
min = n
|
||||
}
|
||||
if n > max {
|
||||
max = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Min:", min)
|
||||
fmt.Println("Max:", max)
|
||||
}
|
||||
10
ch01/getPackage.go
Normal file
10
ch01/getPackage.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mactsouk/go/simpleGitHub"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(simpleGitHub.AddTwo(5, 6))
|
||||
}
|
||||
19
ch01/logFatal.go
Normal file
19
ch01/logFatal.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/syslog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sysLog, err := syslog.New(syslog.LOG_ALERT|syslog.LOG_MAIL, "Some program!")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.SetOutput(sysLog)
|
||||
}
|
||||
|
||||
log.Fatal(sysLog)
|
||||
fmt.Println("Will you see this?")
|
||||
}
|
||||
31
ch01/logFiles.go
Normal file
31
ch01/logFiles.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/syslog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
programName := filepath.Base(os.Args[0])
|
||||
sysLog, err := syslog.New(syslog.LOG_INFO|syslog.LOG_LOCAL7, programName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.SetOutput(sysLog)
|
||||
}
|
||||
log.Println("LOG_INFO + LOG_LOCAL7: Logging in Go!")
|
||||
|
||||
sysLog, err = syslog.New(syslog.LOG_MAIL, "Some program!")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.SetOutput(sysLog)
|
||||
}
|
||||
|
||||
log.Println("LOG_MAIL: Logging in Go!")
|
||||
fmt.Println("Will you see this?")
|
||||
}
|
||||
19
ch01/logPanic.go
Normal file
19
ch01/logPanic.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/syslog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sysLog, err := syslog.New(syslog.LOG_ALERT|syslog.LOG_MAIL, "Some program!")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.SetOutput(sysLog)
|
||||
}
|
||||
|
||||
log.Panic(sysLog)
|
||||
fmt.Println("Will you see this?")
|
||||
}
|
||||
35
ch01/newError.go
Normal file
35
ch01/newError.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func returnError(a, b int) error {
|
||||
if a == b {
|
||||
err := errors.New("Error in returnError() function!")
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := returnError(1, 2)
|
||||
if err == nil {
|
||||
fmt.Println("returnError() ended normally!")
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
err = returnError(10, 10)
|
||||
if err == nil {
|
||||
fmt.Println("returnError() ended normally!")
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
if err.Error() == "Error in returnError() function!" {
|
||||
fmt.Println("!!")
|
||||
}
|
||||
}
|
||||
10
ch01/packageNotUsed.go
Normal file
10
ch01/packageNotUsed.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello there!")
|
||||
}
|
||||
10
ch01/packageNotUsedUnderscore.go
Normal file
10
ch01/packageNotUsedUnderscore.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello there!")
|
||||
}
|
||||
18
ch01/printing.go
Normal file
18
ch01/printing.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v1 := "123"
|
||||
v2 := 123
|
||||
v3 := "Have a nice day\n"
|
||||
v4 := "abc"
|
||||
|
||||
fmt.Print(v1, v2, v3, v4)
|
||||
fmt.Println()
|
||||
fmt.Println(v1, v2, v3, v4)
|
||||
fmt.Print(v1, " ", v2, " ", v3, " ", v4, "\n")
|
||||
fmt.Printf("%s%d %s %s\n", v1, v2, v3, v4)
|
||||
}
|
||||
20
ch01/stdERR.go
Normal file
20
ch01/stdERR.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myString := ""
|
||||
arguments := os.Args
|
||||
if len(arguments) == 1 {
|
||||
myString = "Please give me one argument!"
|
||||
} else {
|
||||
myString = arguments[1]
|
||||
}
|
||||
|
||||
io.WriteString(os.Stdout, "This is Standard output\n")
|
||||
io.WriteString(os.Stderr, myString)
|
||||
io.WriteString(os.Stderr, "\n")
|
||||
}
|
||||
18
ch01/stdIN.go
Normal file
18
ch01/stdIN.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var f *os.File
|
||||
f = os.Stdin
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
fmt.Println(">", scanner.Text())
|
||||
}
|
||||
}
|
||||
19
ch01/stdOUT.go
Normal file
19
ch01/stdOUT.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myString := ""
|
||||
arguments := os.Args
|
||||
if len(arguments) == 1 {
|
||||
myString = "Please give me one argument!"
|
||||
} else {
|
||||
myString = arguments[1]
|
||||
}
|
||||
|
||||
io.WriteString(os.Stdout, myString)
|
||||
io.WriteString(os.Stdout, "\n")
|
||||
}
|
||||
14
ch02/cGo.go
Normal file
14
ch02/cGo.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
//#include <stdio.h>
|
||||
//void callC() {
|
||||
// printf("Calling C code!\n");
|
||||
//}
|
||||
import "C"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("A Go statement!")
|
||||
C.callC()
|
||||
fmt.Println("Another Go statement!")
|
||||
}
|
||||
24
ch02/callC.go
Normal file
24
ch02/callC.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
// #cgo CFLAGS: -I${SRCDIR}/callClib
|
||||
// #cgo LDFLAGS: ${SRCDIR}/callC.a
|
||||
// #include <stdlib.h>
|
||||
// #include <callC.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Going to call a C function!")
|
||||
C.cHello()
|
||||
|
||||
fmt.Println("Going to call another C function!")
|
||||
myMessage := C.CString("This is Mihalis!")
|
||||
defer C.free(unsafe.Pointer(myMessage))
|
||||
C.printMessage(myMessage)
|
||||
|
||||
fmt.Println("All perfectly done!")
|
||||
}
|
||||
10
ch02/callClib/callC.c
Normal file
10
ch02/callClib/callC.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#include "callC.h"
|
||||
|
||||
void cHello() {
|
||||
printf("Hello from C!\n");
|
||||
}
|
||||
|
||||
void printMessage(char* message) {
|
||||
printf("Go send me %s\n", message);
|
||||
}
|
||||
7
ch02/callClib/callC.h
Normal file
7
ch02/callClib/callC.h
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef CALLC_H
|
||||
#define CALLC_H
|
||||
|
||||
void cHello();
|
||||
void printMessage(char* message);
|
||||
|
||||
#endif
|
||||
36
ch02/defer.go
Normal file
36
ch02/defer.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func d1() {
|
||||
for i := 3; i > 0; i-- {
|
||||
defer fmt.Print(i, " ")
|
||||
}
|
||||
}
|
||||
|
||||
func d2() {
|
||||
for i := 3; i > 0; i-- {
|
||||
defer func() {
|
||||
fmt.Print(i, " ")
|
||||
}()
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func d3() {
|
||||
for i := 3; i > 0; i-- {
|
||||
defer func(n int) {
|
||||
fmt.Print(n, " ")
|
||||
}(i)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
d1()
|
||||
d2()
|
||||
fmt.Println()
|
||||
d3()
|
||||
fmt.Println()
|
||||
}
|
||||
38
ch02/gColl.go
Normal file
38
ch02/gColl.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printStats(mem runtime.MemStats) {
|
||||
runtime.ReadMemStats(&mem)
|
||||
fmt.Println("mem.Alloc:", mem.Alloc)
|
||||
fmt.Println("mem.TotalAlloc:", mem.TotalAlloc)
|
||||
fmt.Println("mem.HeapAlloc:", mem.HeapAlloc)
|
||||
fmt.Println("mem.NumGC:", mem.NumGC)
|
||||
fmt.Println("-----")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var mem runtime.MemStats
|
||||
printStats(mem)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
s := make([]byte, 50000000)
|
||||
if s == nil {
|
||||
fmt.Println("Operation failed!")
|
||||
}
|
||||
}
|
||||
printStats(mem)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
s := make([]byte, 100000000)
|
||||
if s == nil {
|
||||
fmt.Println("Operation failed!")
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
printStats(mem)
|
||||
}
|
||||
14
ch02/goEnv.go
Normal file
14
ch02/goEnv.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Print("You are using ", runtime.Compiler, " ")
|
||||
fmt.Println("on a", runtime.GOARCH, "machine")
|
||||
fmt.Println("Using Go version", runtime.Version())
|
||||
fmt.Println("Number of CPUs:", runtime.NumCPU())
|
||||
fmt.Println("Number of Goroutines:", runtime.NumGoroutine())
|
||||
}
|
||||
14
ch02/justPanic.go
Normal file
14
ch02/justPanic.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
panic("Not enough arguments!")
|
||||
}
|
||||
|
||||
fmt.Println("Thanks for the argument(s)!")
|
||||
}
|
||||
24
ch02/moreUnsafe.go
Normal file
24
ch02/moreUnsafe.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := [...]int{0, 1, -2, 3, 4}
|
||||
pointer := &array[0]
|
||||
fmt.Print(*pointer, " ")
|
||||
memoryAddress := uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0])
|
||||
|
||||
for i := 0; i < len(array)-1; i++ {
|
||||
pointer = (*int)(unsafe.Pointer(memoryAddress))
|
||||
fmt.Print(*pointer, " ")
|
||||
memoryAddress = uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0])
|
||||
}
|
||||
fmt.Println()
|
||||
pointer = (*int)(unsafe.Pointer(memoryAddress))
|
||||
fmt.Print("One more: ", *pointer, " ")
|
||||
memoryAddress = uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0])
|
||||
fmt.Println()
|
||||
}
|
||||
9
ch02/nodeTree.go
Normal file
9
ch02/nodeTree.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello there!")
|
||||
}
|
||||
17
ch02/nodeTreeMore.go
Normal file
17
ch02/nodeTreeMore.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func functionOne(x int) {
|
||||
fmt.Println(x)
|
||||
}
|
||||
|
||||
func main() {
|
||||
varOne := 1
|
||||
varTwo := 2
|
||||
fmt.Println("Hello there!")
|
||||
functionOne(varOne)
|
||||
functionOne(varTwo)
|
||||
}
|
||||
BIN
ch02/nodeTreeMore.o
Normal file
BIN
ch02/nodeTreeMore.o
Normal file
Binary file not shown.
29
ch02/panicRecover.go
Normal file
29
ch02/panicRecover.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func a() {
|
||||
fmt.Println("Inside a()")
|
||||
defer func() {
|
||||
if c := recover(); c != nil {
|
||||
fmt.Println("Recover inside a()!")
|
||||
}
|
||||
}()
|
||||
fmt.Println("About to call b()")
|
||||
b()
|
||||
fmt.Println("b() exited!")
|
||||
fmt.Println("Exiting a().")
|
||||
}
|
||||
|
||||
func b() {
|
||||
fmt.Println("Inside b()")
|
||||
panic("Panic in b()!")
|
||||
fmt.Println("Exiting b()")
|
||||
}
|
||||
|
||||
func main() {
|
||||
a()
|
||||
fmt.Println("main() ended!")
|
||||
}
|
||||
23
ch02/requiredVersion.go
Normal file
23
ch02/requiredVersion.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myVersion := runtime.Version()
|
||||
major := strings.Split(myVersion, ".")[0][2]
|
||||
minor := strings.Split(myVersion, ".")[1]
|
||||
m1, _ := strconv.Atoi(string(major))
|
||||
m2, _ := strconv.Atoi(minor)
|
||||
|
||||
if m1 == 1 && m2 < 8 {
|
||||
fmt.Println("Need Go version 1.8 or higher!")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("You are using Go version 1.8 or higher!")
|
||||
}
|
||||
21
ch02/unsafe.go
Normal file
21
ch02/unsafe.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var value int64 = 5
|
||||
var p1 = &value
|
||||
var p2 = (*int32)(unsafe.Pointer(p1))
|
||||
|
||||
fmt.Println("*p1: ", *p1)
|
||||
fmt.Println("*p2: ", *p2)
|
||||
*p1 = 5434123412312431212
|
||||
fmt.Println(value)
|
||||
fmt.Println("*p2: ", *p2)
|
||||
*p1 = 54341234
|
||||
fmt.Println(value)
|
||||
fmt.Println("*p2: ", *p2)
|
||||
}
|
||||
20
ch02/usedByC.go
Normal file
20
ch02/usedByC.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//export PrintMessage
|
||||
func PrintMessage() {
|
||||
fmt.Println("A Go function!")
|
||||
}
|
||||
|
||||
//export Multiply
|
||||
func Multiply(a, b int) int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
func main() {
|
||||
}
|
||||
14
ch02/willUseGo.c
Normal file
14
ch02/willUseGo.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include "usedByC.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
GoInt x = 12;
|
||||
GoInt y = 23;
|
||||
|
||||
printf("About to call a Go function!\n");
|
||||
PrintMessage();
|
||||
GoInt p = Multiply(x,y);
|
||||
printf("Product: %d\n",(int)p);
|
||||
printf("It worked!\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue