Skip to content

Commit d4f5096

Browse files
committed
initial import all sources
1 parent f866520 commit d4f5096

File tree

561 files changed

+17110
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

561 files changed

+17110
-0
lines changed

chapter1/sources/sieve.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
3+
#define LIMIT 50
4+
#define PRIMES 10
5+
6+
void sieve() {
7+
int c, i,j,numbers[LIMIT], primes[PRIMES];
8+
9+
for (i=0;i<LIMIT;i++){
10+
numbers[i]=i+2; /*fill the array with natural numbers*/
11+
}
12+
13+
for (i=0;i<LIMIT;i++){
14+
if (numbers[i]!=-1){
15+
for (j=2*numbers[i]-2;j<LIMIT;j+=numbers[i])
16+
numbers[j]=-1; /*sieve the non-primes*/
17+
}
18+
}
19+
20+
c = j = 0;
21+
for (i=0;i<LIMIT&&j<PRIMES;i++) {
22+
if (numbers[i]!=-1) {
23+
primes[j++] = numbers[i]; /*transfer the primes to their own array*/
24+
c++;
25+
}
26+
}
27+
28+
for (i=0;i<c;i++) printf("%d\n",primes[i]);
29+
}
30+
31+
int main() {
32+
sieve();
33+
}

chapter1/sources/sieve.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
// Send the sequence 2, 3, 4, ... to channel 'ch'.
4+
func Generate(ch chan<- int) {
5+
for i := 2; ; i++ {
6+
ch <- i // Send 'i' to channel 'ch'.
7+
}
8+
}
9+
10+
// Copy the values from channel 'in' to channel 'out',
11+
// removing those divisible by 'prime'.
12+
func Filter(in <-chan int, out chan<- int, prime int) {
13+
for {
14+
i := <-in // Receive value from 'in'.
15+
if i%prime != 0 {
16+
out <- i // Send 'i' to 'out'.
17+
}
18+
}
19+
}
20+
21+
// The prime sieve: Daisy-chain Filter processes.
22+
func main() {
23+
ch := make(chan int) // Create a new channel.
24+
go Generate(ch) // Launch Generate goroutine.
25+
for i := 0; i < 10; i++ {
26+
prime := <-ch
27+
print(prime, "\n")
28+
ch1 := make(chan int)
29+
go Filter(ch, ch1, prime)
30+
ch = ch1
31+
}
32+
}

chapter1/sources/sieve.hs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sieve [] = []
2+
sieve (x:xs) = x : sieve (filter (\a -> not $ a `mod` x == 0) xs)
3+
4+
n = 100
5+
main = print $ sieve [2..n]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
//go:generate go-bindata -o static.go static/img/go-mascot.jpg
9+
10+
func main() {
11+
data, err := Asset("static/img/go-mascot.jpg")
12+
if err != nil {
13+
fmt.Println("Asset invoke error:", err)
14+
return
15+
}
16+
17+
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
18+
w.Write(data)
19+
})
20+
21+
http.ListenAndServe(":8080", nil)
22+
}

chapter10/sources/go-generate/bindata-demo/static.go

+248
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Weekday int
6+
7+
const (
8+
Sunday Weekday = iota
9+
Monday
10+
Tuesday
11+
Wednesday
12+
Thursday
13+
Friday
14+
Saturday
15+
)
16+
17+
func (d Weekday) String() string {
18+
switch d {
19+
case Sunday:
20+
return "Sunday"
21+
case Monday:
22+
return "Monday"
23+
case Tuesday:
24+
return "Tuesday"
25+
case Wednesday:
26+
return "Wednesday"
27+
case Thursday:
28+
return "Thursday"
29+
case Friday:
30+
return "Friday"
31+
case Saturday:
32+
return "Saturday"
33+
}
34+
35+
return "Sunday" //default 0 -> "Sunday"
36+
}
37+
38+
func main() {
39+
var d Weekday
40+
fmt.Println(d)
41+
fmt.Println(Weekday(1))
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/bigwhite/generate-args-demo
2+
3+
go 1.14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:generate pwd
2+
package main
3+
4+
func main() {
5+
println("hello, go generate")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:generate pwd
2+
package subpkg1
3+
4+
func F() {
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:generate pwd
2+
package subpkg2
3+
4+
func F() {
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:generate echo "top"
2+
package main
3+
4+
import "fmt"
5+
6+
//go:generate echo "middle"
7+
func main() {
8+
fmt.Println("hello, go generate")
9+
}
10+
11+
//go:generate echo "tail"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
package msg;
4+
5+
message request {
6+
string msgID = 1;
7+
string field1 = 2;
8+
repeated string field2 = 3;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all: build
2+
3+
build: gen-protobuf
4+
go build
5+
6+
gen-protobuf:
7+
protoc -I ./IDL msg.proto --gofast_out=./msg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/bigwhite/protobuf-demo
2+
3+
go 1.14
4+
5+
require github.com/golang/protobuf v1.4.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
2+
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
3+
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
4+
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
5+
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
6+
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
7+
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
8+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
9+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
10+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
11+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
12+
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
13+
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
14+
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
15+
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
16+
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
17+
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
18+
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
msg "github.com/bigwhite/protobuf-demo/msg"
7+
)
8+
9+
//go:generate protoc -I ./IDL msg.proto --gofast_out=./msg
10+
func main() {
11+
var m = msg.Request{
12+
MsgID: "xxxx",
13+
Field1: "field1",
14+
Field2: []string{"field2-1", "field2-2"},
15+
}
16+
fmt.Println(m)
17+
}

0 commit comments

Comments
 (0)