The document contains Go code snippets demonstrating various Go language features including:
1. Setting up a Go development environment and compiling a simple "Hello World" program.
2. Defining structs, slices, maps and functions.
3. Using channels and goroutines for concurrency.
4. Implementing interfaces and polymorphism.
5. Recursive and anonymous functions.
6. Demonstrating differences between arrays and slices.
21. package main
import "fmt"
func main() {
str := "Hello";
for i := 0; i < len(str); i += 1 {
fmt.Printf("> %cn", str[i]);
}
}
22. package main
import "fmt"
func main() {
str := "Hello";
i := 0;
for {
if i >= len(str) {
break
}
fmt.Printf("> %cn", str[i]);
i += 1;
}
}
23. package main
import "bufio"
import "os"
import "fmt"
func main() {
in := bufio.NewReader(os.Stdin);
for {
str, e := in.ReadString('n');
if e != nil {
if e != os.EOF {
fmt.Printf("Error: %sn", e);
}
break
}
os.Stdout.WriteString("-> ");
os.Stdout.WriteString(str);
}
}
24. package main
import "fmt"
import "regexp"
func main() {
re, e := regexp.Compile("[A-Z][a-z]+");
if e != nil {
fmt.Printf("Error: %sn", e);
return
}
fmt.Printf("%tn", re.MatchString("test"));
matches := re.MatchStrings("Abc Def ");
for i := range matches {
fmt.Printf("%sn", matches[i]);
}
str := "test Test abc";
fmt.Printf("%sn", re.ReplaceAllString(str , "***"));
}
25. package main
import (
"io";
"fmt";
"http"
)
func main() {
url := "http://search.twitter.com/search.json?q="
+ http.URLEscape("#moriyoshi");
resp, _, e := http.Get(url);
if e != nil { goto Error; }
body, e := io.ReadAll(resp.Body);
if e != nil { goto Error; }
resp.Body.Close();
fmt.Printf(">>> %sn", string(body));
return;
Error:
fmt.Printf("Error: %sn", e);
return;
}
26. package main
import (
"io";
"fmt";
"http";
"json";
)
func jsonGet(url string) json.Json {
resp, _, e := http.Get(url);
if e != nil { goto Error; }
body, e := io.ReadAll(resp.Body);
if e != nil { goto Error; }
resp.Body.Close();
jsonObj, ok, etok := json.StringToJson(string(body));
if !ok {
fmt.Printf("JSON syntax error near: " + etok);
return nil;
}
return jsonObj;
Error:
fmt.Printf("Error: %sn", e);
return nil;
}
27. func main() {
url := "http://search.twitter.com/search.json?q="
+ http.URLEscape("#moriyoshi");
j := jsonGet(url);
results := json.Walk(j, "results");
for i := 0; i < results.Len(); i += 1 {
r := results.Elem(i);
fmt.Printf("%s: %sn",
r.Get("from_user"),
r.Get("text"));
}
return;
}
28. package main
import (
"fmt";
"time";
)
func sub(n int) {
fmt.Printf("Goroutine #%d started.n", n);
for i := 0; i < 5; i += 1 {
fmt.Printf("%d: %dn", n, i);
time.Sleep(1e9);
}
}
func main() {
for i := 0; i < 10; i += 1 {
go sub(i);
}
time.Sleep(5e9);
}
29. package main
import (
"fmt";
"time";
)
func sub(n int, ch chan int) {
fmt.Printf("Goroutine #%d started.n",
n);
for i := 0; i < 5; i += 1 {
fmt.Printf("%d: %dn", n, i);
time.Sleep(1e8);
}
ch <- n
}
30. func main() {
ch := make(chan int);
for n := 0; n < 10; n += 1 {
go sub(n, ch);
time.Sleep(4e8)
}
for i := 0; i < 10; i += 1 {
n := <- ch;
fmt.Printf("Goroutine #%d ended.
n", n)
}
}
31. package main
import (
"fmt";
)
type Employee struct {
name string;
}
type Company struct {
name string;
employees []Employee;
}
func main() {
c := Company { name:"Acme" };
c.employees = []Employee {
Employee { name:"Foo" },
Employee { name:"Bar" }
};
fmt.Printf("Company name: %sn", c.name);
fmt.Printf("Employees: %dn", len(c.employees));
for _, employee := range c.employees {
fmt.Printf(" %sn", employee.name);
}
}
32. package main
import (
"fmt";
)
func main() {
m := make(map[string] string);
m["Foo"] = "Hoge";
m["Bar"] = "Fuga";
for k, v := range m {
fmt.Printf("%s: %sn", k, v)
}
}
33. package main
import (
"fmt";
)
type MyInt int;
type Foo struct {
values []int;
}
func (self *Foo) sum() int {
retval := 0;
for _, v := range self.values {
retval += v
}
return retval
}
func main() {
f := &Foo { values: []int { 0, 1, 2, 3, 4 } };
fmt.Printf("%dn", f.sum());
}