1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-24 04:22:22 +00:00

ch1.2: 重新准备代码

This commit is contained in:
chai2010 2018-12-16 12:17:32 +08:00
parent 2555e3e04f
commit 673c263fa6
22 changed files with 243 additions and 0 deletions

View 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);
}

View 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';

View File

@ -0,0 +1,4 @@
main()
{
printf("hello, world");
}

View File

@ -0,0 +1,4 @@
main()
{
printf("hello, world\n");
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
main()
{
printf("hello, world\n");
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
main(void)
{
printf("hello, world\n");
}

View File

@ -0,0 +1,8 @@
// +build ignore
package main
func main() int {
print "hello, world\n";
return 0;
}

View File

@ -0,0 +1,7 @@
// +build ignore
package main
func main() {
print "hello, world\n";
}

View File

@ -0,0 +1,5 @@
package main
func main() {
print("hello, world\n");
}

View File

@ -0,0 +1,9 @@
// +build ignore
package main
import "fmt"
func main() {
fmt.printf("hello, world\n");
}

View File

@ -0,0 +1,9 @@
// +build ignore
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n");
}

View File

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}

View File

@ -0,0 +1,6 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package main
func main()

View File

@ -0,0 +1,19 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
#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

View File

@ -0,0 +1,16 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
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)
}

View File

@ -0,0 +1,8 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
#include <iostream>
void SayHello() {
std::cout << "Hello, World!" << std::endl;
}

View File

@ -0,0 +1,14 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// +build ignore
package main
import (
hello "."
)
func main() {
hello.SayHello()
}

View File

@ -0,0 +1,8 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
%module main
%inline %{
extern void SayHello();
%}

View File

@ -0,0 +1,23 @@
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
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)
}
}

View 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");
}

View File

@ -0,0 +1 @@
print("Hello,", "World", "\n");

View 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();