mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
ch1-02: 添加例子代码
This commit is contained in:
parent
7a095493b3
commit
53c151016a
19
examples/ch1-02/hello-alef/hello.alef
Normal file
19
examples/ch1-02/hello-alef/hello.alef
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <alef.h>
|
||||||
|
|
||||||
|
void receive(chan(byte*) c) {
|
||||||
|
byte *s;
|
||||||
|
s = <- c;
|
||||||
|
print("%s\n", s);
|
||||||
|
terminate(nil);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
chan(byte*) c;
|
||||||
|
alloc c;
|
||||||
|
proc receive(c);
|
||||||
|
task receive(c);
|
||||||
|
c <- = "hello proc or task";
|
||||||
|
c <- = "hello proc or task";
|
||||||
|
print("done\n");
|
||||||
|
terminate(nil);
|
||||||
|
}
|
8
examples/ch1-02/hello-b/main.b
Normal file
8
examples/ch1-02/hello-b/main.b
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
main() {
|
||||||
|
extrn a, b, c;
|
||||||
|
putchar(a); putchar(b); putchar(c);
|
||||||
|
putchar('!*n');
|
||||||
|
}
|
||||||
|
a 'hell';
|
||||||
|
b 'o, w';
|
||||||
|
c 'orld';
|
4
examples/ch1-02/hello-c-01/hello-c-01.c
Normal file
4
examples/ch1-02/hello-c-01/hello-c-01.c
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
main()
|
||||||
|
{
|
||||||
|
printf("hello, world");
|
||||||
|
}
|
4
examples/ch1-02/hello-c-02/hello-c-02.c
Normal file
4
examples/ch1-02/hello-c-02/hello-c-02.c
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
main()
|
||||||
|
{
|
||||||
|
printf("hello, world\n");
|
||||||
|
}
|
6
examples/ch1-02/hello-c-03/hello-c-03.c
Normal file
6
examples/ch1-02/hello-c-03/hello-c-03.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
printf("hello, world\n");
|
||||||
|
}
|
6
examples/ch1-02/hello-c-04/hello-c-04.c
Normal file
6
examples/ch1-02/hello-c-04/hello-c-04.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
printf("hello, world\n");
|
||||||
|
}
|
8
examples/ch1-02/hello-go-200806/hello.go.txt
Normal file
8
examples/ch1-02/hello-go-200806/hello.go.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() int {
|
||||||
|
print "hello, world\n";
|
||||||
|
return 0;
|
||||||
|
}
|
7
examples/ch1-02/hello-go-20080627/hello.go.txt
Normal file
7
examples/ch1-02/hello-go-20080627/hello.go.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print "hello, world\n";
|
||||||
|
}
|
5
examples/ch1-02/hello-go-20080811/hello.go.txt
Normal file
5
examples/ch1-02/hello-go-20080811/hello.go.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print("hello, world\n");
|
||||||
|
}
|
9
examples/ch1-02/hello-go-20081024/hello.go.txt
Normal file
9
examples/ch1-02/hello-go-20081024/hello.go.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.printf("hello, world\n");
|
||||||
|
}
|
9
examples/ch1-02/hello-go-20090915/hello.go.txt
Normal file
9
examples/ch1-02/hello-go-20090915/hello.go.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Printf("hello, world\n");
|
||||||
|
}
|
7
examples/ch1-02/hello-go-20091211/hello.go
Normal file
7
examples/ch1-02/hello-go-20091211/hello.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Printf("hello, world\n")
|
||||||
|
}
|
3
examples/ch1-02/hello-go-asm/hello.go
Normal file
3
examples/ch1-02/hello-go-asm/hello.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main()
|
16
examples/ch1-02/hello-go-asm/hello_amd64.s
Normal file
16
examples/ch1-02/hello-go-asm/hello_amd64.s
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include "textflag.h"
|
||||||
|
#include "funcdata.h"
|
||||||
|
|
||||||
|
// "Hello World!\n"
|
||||||
|
DATA text<>+0(SB)/8,$"Hello Wo"
|
||||||
|
DATA text<>+8(SB)/8,$"rld!\n"
|
||||||
|
GLOBL text<>(SB),NOPTR,$16
|
||||||
|
|
||||||
|
// func main()
|
||||||
|
TEXT ·main(SB), $16-0
|
||||||
|
NO_LOCAL_POINTERS
|
||||||
|
MOVQ $text<>+0(SB), AX
|
||||||
|
MOVQ AX, (SP)
|
||||||
|
MOVQ $16, 8(SP)
|
||||||
|
CALL runtime·printstring(SB)
|
||||||
|
RET
|
13
examples/ch1-02/hello-go-cgo/hello.go
Normal file
13
examples/ch1-02/hello-go-cgo/hello.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// #include <stdio.h>
|
||||||
|
// #include <stdlib.h>
|
||||||
|
import "C"
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
msg := C.CString("Hello, World!\n")
|
||||||
|
defer C.free(unsafe.Pointer(msg))
|
||||||
|
|
||||||
|
C.fputs(msg, C.stdout)
|
||||||
|
}
|
5
examples/ch1-02/hello-go-swig/hello.cc
Normal file
5
examples/ch1-02/hello-go-swig/hello.cc
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void SayHello() {
|
||||||
|
std::cout << "Hello, World!" << std::endl;
|
||||||
|
}
|
11
examples/ch1-02/hello-go-swig/hello.go
Normal file
11
examples/ch1-02/hello-go-swig/hello.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
hello "."
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hello.SayHello()
|
||||||
|
}
|
5
examples/ch1-02/hello-go-swig/hello.swigcxx
Normal file
5
examples/ch1-02/hello-go-swig/hello.swigcxx
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
%module main
|
||||||
|
|
||||||
|
%inline %{
|
||||||
|
extern void SayHello();
|
||||||
|
%}
|
20
examples/ch1-02/hello-go-v2/hello.go
Normal file
20
examples/ch1-02/hello-go-v2/hello.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Please visit http://127.0.0.1:12345/")
|
||||||
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
s := fmt.Sprintf("你好, 世界! -- Time: %s", time.Now().String())
|
||||||
|
fmt.Fprintf(w, "%v\n", s)
|
||||||
|
log.Printf("%v\n", s)
|
||||||
|
})
|
||||||
|
if err := http.ListenAndServe(":12345", nil); err != nil {
|
||||||
|
log.Fatal("ListenAndServe: ", err)
|
||||||
|
}
|
||||||
|
}
|
15
examples/ch1-02/hello-limbo/hello.limbo
Normal file
15
examples/ch1-02/hello-limbo/hello.limbo
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
implement Hello;
|
||||||
|
|
||||||
|
include "sys.m"; sys: Sys;
|
||||||
|
include "draw.m";
|
||||||
|
|
||||||
|
Hello: module
|
||||||
|
{
|
||||||
|
init: fn(ctxt: ref Draw->Context, args: list of string);
|
||||||
|
};
|
||||||
|
|
||||||
|
init(ctxt: ref Draw->Context, args: list of string)
|
||||||
|
{
|
||||||
|
sys = load Sys Sys->PATH;
|
||||||
|
sys->print("hello, world\n");
|
||||||
|
}
|
1
examples/ch1-02/hello-newsqueak/hello.newsqueak
Normal file
1
examples/ch1-02/hello-newsqueak/hello.newsqueak
Normal file
@ -0,0 +1 @@
|
|||||||
|
print("Hello,", "World", "\n");
|
41
examples/ch1-02/prime-newsqueak/prime.newsqueak
Normal file
41
examples/ch1-02/prime-newsqueak/prime.newsqueak
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// 向管道输出从2开始的自然数序列
|
||||||
|
counter := prog(c:chan of int) {
|
||||||
|
i := 2;
|
||||||
|
for(;;) {
|
||||||
|
c <-= i++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 针对listen管道获取的数列,过滤掉是prime倍数的数
|
||||||
|
// 新的序列输出到send管道
|
||||||
|
filter := prog(prime:int, listen, send:chan of int) {
|
||||||
|
i:int;
|
||||||
|
for(;;) {
|
||||||
|
if((i = <-listen)%prime) {
|
||||||
|
send <-= i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 主函数
|
||||||
|
// 每个管道第一个流出的数必然是素数
|
||||||
|
// 然后基于这个新的素数构建新的素数过滤器
|
||||||
|
sieve := prog() of chan of int {
|
||||||
|
c := mk(chan of int);
|
||||||
|
begin counter(c);
|
||||||
|
prime := mk(chan of int);
|
||||||
|
begin prog(){
|
||||||
|
p:int;
|
||||||
|
newc:chan of int;
|
||||||
|
for(;;){
|
||||||
|
prime <-= p =<- c;
|
||||||
|
newc = mk();
|
||||||
|
begin filter(p, c, newc);
|
||||||
|
c = newc;
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
become prime;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 启动素数筛
|
||||||
|
prime := sieve();
|
Loading…
x
Reference in New Issue
Block a user