From 4e366cd04a49b959fab9541eb5db880c617da08b Mon Sep 17 00:00:00 2001 From: Mihalis Tsoukalos Date: Thu, 10 Jan 2019 09:18:16 +0200 Subject: [PATCH] Code for ch01 + ch02 --- ch01/aSourceFile.go | 9 +++++++ ch01/cla.go | 31 ++++++++++++++++++++++ ch01/curly.go | 11 ++++++++ ch01/errors.go | 44 +++++++++++++++++++++++++++++++ ch01/getPackage.go | 10 +++++++ ch01/logFatal.go | 19 +++++++++++++ ch01/logFiles.go | 31 ++++++++++++++++++++++ ch01/logPanic.go | 19 +++++++++++++ ch01/newError.go | 35 ++++++++++++++++++++++++ ch01/packageNotUsed.go | 10 +++++++ ch01/packageNotUsedUnderscore.go | 10 +++++++ ch01/printing.go | 18 +++++++++++++ ch01/stdERR.go | 20 ++++++++++++++ ch01/stdIN.go | 18 +++++++++++++ ch01/stdOUT.go | 19 +++++++++++++ ch02/cGo.go | 14 ++++++++++ ch02/callC.go | 24 +++++++++++++++++ ch02/callClib/callC.c | 10 +++++++ ch02/callClib/callC.h | 7 +++++ ch02/defer.go | 36 +++++++++++++++++++++++++ ch02/gColl.go | 38 ++++++++++++++++++++++++++ ch02/goEnv.go | 14 ++++++++++ ch02/justPanic.go | 14 ++++++++++ ch02/moreUnsafe.go | 24 +++++++++++++++++ ch02/nodeTree.go | 9 +++++++ ch02/nodeTreeMore.go | 17 ++++++++++++ ch02/nodeTreeMore.o | Bin 0 -> 2850 bytes ch02/panicRecover.go | 29 ++++++++++++++++++++ ch02/requiredVersion.go | 23 ++++++++++++++++ ch02/unsafe.go | 21 +++++++++++++++ ch02/usedByC.go | 20 ++++++++++++++ ch02/willUseGo.c | 14 ++++++++++ 32 files changed, 618 insertions(+) create mode 100644 ch01/aSourceFile.go create mode 100644 ch01/cla.go create mode 100644 ch01/curly.go create mode 100644 ch01/errors.go create mode 100644 ch01/getPackage.go create mode 100644 ch01/logFatal.go create mode 100644 ch01/logFiles.go create mode 100644 ch01/logPanic.go create mode 100644 ch01/newError.go create mode 100644 ch01/packageNotUsed.go create mode 100644 ch01/packageNotUsedUnderscore.go create mode 100644 ch01/printing.go create mode 100644 ch01/stdERR.go create mode 100644 ch01/stdIN.go create mode 100644 ch01/stdOUT.go create mode 100644 ch02/cGo.go create mode 100644 ch02/callC.go create mode 100644 ch02/callClib/callC.c create mode 100644 ch02/callClib/callC.h create mode 100644 ch02/defer.go create mode 100644 ch02/gColl.go create mode 100644 ch02/goEnv.go create mode 100644 ch02/justPanic.go create mode 100644 ch02/moreUnsafe.go create mode 100644 ch02/nodeTree.go create mode 100644 ch02/nodeTreeMore.go create mode 100644 ch02/nodeTreeMore.o create mode 100644 ch02/panicRecover.go create mode 100644 ch02/requiredVersion.go create mode 100644 ch02/unsafe.go create mode 100644 ch02/usedByC.go create mode 100644 ch02/willUseGo.c diff --git a/ch01/aSourceFile.go b/ch01/aSourceFile.go new file mode 100644 index 0000000..795a763 --- /dev/null +++ b/ch01/aSourceFile.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("This is a sample Go program!") +} diff --git a/ch01/cla.go b/ch01/cla.go new file mode 100644 index 0000000..46c411b --- /dev/null +++ b/ch01/cla.go @@ -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) +} diff --git a/ch01/curly.go b/ch01/curly.go new file mode 100644 index 0000000..e64d759 --- /dev/null +++ b/ch01/curly.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" +) + +func main() +{ + fmt.Println("Go has strict rules for curly braces!") +} + diff --git a/ch01/errors.go b/ch01/errors.go new file mode 100644 index 0000000..2402424 --- /dev/null +++ b/ch01/errors.go @@ -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) +} diff --git a/ch01/getPackage.go b/ch01/getPackage.go new file mode 100644 index 0000000..7bde033 --- /dev/null +++ b/ch01/getPackage.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "github.com/mactsouk/go/simpleGitHub" +) + +func main() { + fmt.Println(simpleGitHub.AddTwo(5, 6)) +} diff --git a/ch01/logFatal.go b/ch01/logFatal.go new file mode 100644 index 0000000..fc32f24 --- /dev/null +++ b/ch01/logFatal.go @@ -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?") +} diff --git a/ch01/logFiles.go b/ch01/logFiles.go new file mode 100644 index 0000000..c17d721 --- /dev/null +++ b/ch01/logFiles.go @@ -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?") +} diff --git a/ch01/logPanic.go b/ch01/logPanic.go new file mode 100644 index 0000000..6b1e4ad --- /dev/null +++ b/ch01/logPanic.go @@ -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?") +} diff --git a/ch01/newError.go b/ch01/newError.go new file mode 100644 index 0000000..568489b --- /dev/null +++ b/ch01/newError.go @@ -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("!!") + } +} diff --git a/ch01/packageNotUsed.go b/ch01/packageNotUsed.go new file mode 100644 index 0000000..1297c83 --- /dev/null +++ b/ch01/packageNotUsed.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + fmt.Println("Hello there!") +} diff --git a/ch01/packageNotUsedUnderscore.go b/ch01/packageNotUsedUnderscore.go new file mode 100644 index 0000000..d164c3d --- /dev/null +++ b/ch01/packageNotUsedUnderscore.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + _ "os" +) + +func main() { + fmt.Println("Hello there!") +} diff --git a/ch01/printing.go b/ch01/printing.go new file mode 100644 index 0000000..68bb15f --- /dev/null +++ b/ch01/printing.go @@ -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) +} diff --git a/ch01/stdERR.go b/ch01/stdERR.go new file mode 100644 index 0000000..fcc7261 --- /dev/null +++ b/ch01/stdERR.go @@ -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") +} diff --git a/ch01/stdIN.go b/ch01/stdIN.go new file mode 100644 index 0000000..991d43b --- /dev/null +++ b/ch01/stdIN.go @@ -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()) + } +} diff --git a/ch01/stdOUT.go b/ch01/stdOUT.go new file mode 100644 index 0000000..3345ee4 --- /dev/null +++ b/ch01/stdOUT.go @@ -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") +} diff --git a/ch02/cGo.go b/ch02/cGo.go new file mode 100644 index 0000000..32b0cc9 --- /dev/null +++ b/ch02/cGo.go @@ -0,0 +1,14 @@ +package main + +//#include +//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!") +} diff --git a/ch02/callC.go b/ch02/callC.go new file mode 100644 index 0000000..f6b35ba --- /dev/null +++ b/ch02/callC.go @@ -0,0 +1,24 @@ +package main + +// #cgo CFLAGS: -I${SRCDIR}/callClib +// #cgo LDFLAGS: ${SRCDIR}/callC.a +// #include +// #include +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!") +} diff --git a/ch02/callClib/callC.c b/ch02/callClib/callC.c new file mode 100644 index 0000000..0107c15 --- /dev/null +++ b/ch02/callClib/callC.c @@ -0,0 +1,10 @@ +#include +#include "callC.h" + +void cHello() { + printf("Hello from C!\n"); +} + +void printMessage(char* message) { + printf("Go send me %s\n", message); +} \ No newline at end of file diff --git a/ch02/callClib/callC.h b/ch02/callClib/callC.h new file mode 100644 index 0000000..db1261b --- /dev/null +++ b/ch02/callClib/callC.h @@ -0,0 +1,7 @@ +#ifndef CALLC_H +#define CALLC_H + +void cHello(); +void printMessage(char* message); + +#endif \ No newline at end of file diff --git a/ch02/defer.go b/ch02/defer.go new file mode 100644 index 0000000..570e5f4 --- /dev/null +++ b/ch02/defer.go @@ -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() +} diff --git a/ch02/gColl.go b/ch02/gColl.go new file mode 100644 index 0000000..84432c1 --- /dev/null +++ b/ch02/gColl.go @@ -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) +} diff --git a/ch02/goEnv.go b/ch02/goEnv.go new file mode 100644 index 0000000..d7db9fa --- /dev/null +++ b/ch02/goEnv.go @@ -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()) +} diff --git a/ch02/justPanic.go b/ch02/justPanic.go new file mode 100644 index 0000000..af208b9 --- /dev/null +++ b/ch02/justPanic.go @@ -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)!") +} diff --git a/ch02/moreUnsafe.go b/ch02/moreUnsafe.go new file mode 100644 index 0000000..7096fe9 --- /dev/null +++ b/ch02/moreUnsafe.go @@ -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() +} diff --git a/ch02/nodeTree.go b/ch02/nodeTree.go new file mode 100644 index 0000000..c174829 --- /dev/null +++ b/ch02/nodeTree.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello there!") +} diff --git a/ch02/nodeTreeMore.go b/ch02/nodeTreeMore.go new file mode 100644 index 0000000..a2b560b --- /dev/null +++ b/ch02/nodeTreeMore.go @@ -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) +} diff --git a/ch02/nodeTreeMore.o b/ch02/nodeTreeMore.o new file mode 100644 index 0000000000000000000000000000000000000000..f633ae602eb8a9f075f3f97715ce718cc9d9bb7e GIT binary patch literal 2850 zcmYe#SIAGwN=+_NNJ%Uz&&*Ru%uO*fQAp1>)U(txQi!lhD@x2wEy&NzD@iTl%1zA7 zgD)#c*iQc-c@DoZUY&dkqKFy-Q6U}9tkNi%TPGcn5AB$k%sr>Ewn7A2OXrr5D( z=4F;JFfed4aWgP7aH*(paVc^!Fr?=jTIQrMa;4>#=p{1zlTlLAODoMwF3HT#^Uq6V z_$N?OS&*uinODN_PpPOhuOu@!RWCU|uPnsK)y#z9pBPAAKv8C1NlqTaKLePG-29@{ z;*!MV?D)L=I4F5#*Ku#*jEr>T{_@@CjINC54?5(uKX{GeJ?DYGh=VVv?9@ zl4zcsm||gJY-((1m~574VrF5SY-p0k@UMk2JwGioCsj{RKeRZts8~O@q&UAcTi+$M zIJ+djKtDIJxFog6JzqaLLq9pgNIyA0B~?E!KP5G!C^gkLzbI8NJ)hwppOTUuIKcmj zfqhe4Qk0pO4hhBLlEjkCWRPbKP~2u_nQUloYMhi}nr2~WYG9aRX=-R}lwx3!m}X#N z0t!=*so==^C#9sM2V$k<=cOLnjuZnW8AbW!AQ^^#0-zXxFp+}G*f=>QH90vsIW;NO z!ob+bGR-h8B`G!4Jk8X?+$1@P;h!EX0MOD2!#|z$e7(%Pw0u4Ev>*nP04E2%^nAUd z#Ju!W40Q(S`FfC`&{OhA&B@7ED9K1IN>x;1_$LQ50v>V-$i_ed6Il_&XDEswLL%T0 zFU`y=u|Nu#^yH+>l43mrLxz7=UHro+HV!qb=ra7% z0xQwN$U_iuz0BN#{GyVA#F7j>P#R(QKSwZ+jf00R)uWq7m4ShQ!NWRHfWNH?#A<#M z;L(|*;^EN?BD!5vB0M@>R17?hyQo-zl^%CdaR4)VS;15%i;71tNJXcM3dakuP`8VU zfJe8FiUdfjg-554iUUZAg$GFOaTgU1MvzU%T~q`>45%KEbjrc@>_W-bPX`HTzmBfSm(( zI?Tl&*CN;!Ah#LdbK7wi6@){Nv#2mJFff3f>Cv5|;^5JI!~^Q;Y>=ykCoq=xgIqn8 zkwHS1&5MbbiI<6sn~Q;ADkB3!5(_V*3?sVRT|JuLaCmg;sCe}1sPJzChpY`_8K+0L zj*3U~5st&9>|hJ|xsQS67{NRUn+dK6EckjUldym^Kcj#OKL-O-Gy_XC14A?egCUbL zBRc~VT22Oq4>D$)!^m2}z&HmihMZ$T8o_xIEQ3Ta&S3(D# zIWbO?fZ7eyS1|dNJ`)FrI8+!9C4ioZVMf5{{md&&iE#ofP+)Yx#OPm492|mp>_(9S zC3uJcb6Rc*D8)lWKr9euUMFufq|ickwcDwWf>!f00Ube z6Ndl;#~KjHC9r^zfq{*OfrW)no=HGRpNU0?Lx_Wkk3~d`fq_Ya;UA+E!zw0*RZM(f zV^TQe7-VWVJ0S2`a4mn1Z29OCFJ&YjsDiE8C0TfhXY;0^yd@Nd83=B*<4F5Rv7@`=W z*c1eq7?=zMnD#L-1#nm}82sZj0*B2%CKEOZ1__3L9A*qw3|1^c0!$22N&-x)n56!3 zf{ROre;ihDDJ3NVrfrN$|2S=+y5$%YWE7Yf1eh31{;{brFxfFM{9|%pV`5-p_{ZeL zq`;uS@Q=xbtAL?^!$5#(0i&CP026~l00$R?Q-S~!gU3H6FQ{SJPiMs fM3_VvL>T@ti9u-vCIJQkhJQ?2VEX@mNb?i`YD$l1 literal 0 HcmV?d00001 diff --git a/ch02/panicRecover.go b/ch02/panicRecover.go new file mode 100644 index 0000000..1dd34e2 --- /dev/null +++ b/ch02/panicRecover.go @@ -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!") +} diff --git a/ch02/requiredVersion.go b/ch02/requiredVersion.go new file mode 100644 index 0000000..56b4b6d --- /dev/null +++ b/ch02/requiredVersion.go @@ -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!") +} diff --git a/ch02/unsafe.go b/ch02/unsafe.go new file mode 100644 index 0000000..9b6921d --- /dev/null +++ b/ch02/unsafe.go @@ -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) +} diff --git a/ch02/usedByC.go b/ch02/usedByC.go new file mode 100644 index 0000000..ab38ee7 --- /dev/null +++ b/ch02/usedByC.go @@ -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() { +} diff --git a/ch02/willUseGo.c b/ch02/willUseGo.c new file mode 100644 index 0000000..fd6c59e --- /dev/null +++ b/ch02/willUseGo.c @@ -0,0 +1,14 @@ +#include +#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; +} \ No newline at end of file