mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
examples: 删除冗余代码
This commit is contained in:
parent
6a0c1697f8
commit
c1eb40a914
@ -1,10 +0,0 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("你好, 世界!")
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#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);
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
main() {
|
||||
extrn a, b, c;
|
||||
putchar(a); putchar(b); putchar(c);
|
||||
putchar('!*n');
|
||||
}
|
||||
a 'hell';
|
||||
b 'o, w';
|
||||
c 'orld';
|
@ -1,4 +0,0 @@
|
||||
main()
|
||||
{
|
||||
printf("hello, world");
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
main()
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
main()
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
main(void)
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
func main() int {
|
||||
print "hello, world\n";
|
||||
return 0;
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
print "hello, world\n";
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
print("hello, world\n");
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.printf("hello, world\n");
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Printf("hello, world\n");
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Printf("hello, world\n")
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package main
|
||||
|
||||
func main()
|
@ -1,19 +0,0 @@
|
||||
// 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
|
@ -1,16 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
// 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;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
// 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()
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
%module main
|
||||
|
||||
%inline %{
|
||||
extern void SayHello();
|
||||
%}
|
@ -1,23 +0,0 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
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 +0,0 @@
|
||||
print("Hello,", "World", "\n");
|
@ -1,41 +0,0 @@
|
||||
// 向管道输出从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