Skip to content

Commit

Permalink
Updated up to release of 10/09/29
Browse files Browse the repository at this point in the history
  • Loading branch information
cthulhu committed Oct 2, 2010
1 parent 38cccd5 commit 38f1ab3
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 53 deletions.
2 changes: 1 addition & 1 deletion 01_hello_world/Makefile
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

TARG=hello_world
GOFILES=hello_world.go
Expand Down
2 changes: 1 addition & 1 deletion 02_functions/Makefile
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

TARG=functions
GOFILES=functions.go
Expand Down
36 changes: 5 additions & 31 deletions 03_packages_basics/Makefile
@@ -1,36 +1,10 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

.SUFFIXES: .go .$O
TARG=factorial_example

OBJS=factorial.$O main.$O
TARG=factorial
GOFILES=\
main.go\

GOFILES=factorial.go
include $(GOROOT)/src/Make.cmd

factorial: $(OBJS)
$(LD) -o factorial $(OBJS)

clean:
rm -f $(OBJS) factorial

.go.$O:
$(GC) $<

test:
gotest

testpackage-clean:
rm -f _test/$(TARG).a _gotest_.$O

testpackage: _test/$(TARG).a

_test/$(TARG).a: _gotest_.$O $(OFILES)
@mkdir -p _test/$(dir)
rm -f _test/$(TARG).a
gopack grc $@ _gotest_.$O $(OFILES)

_gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
$(GC) -o $@ $(GOFILES) $(GOTESTFILES)

importpath:
@echo $(TARG)
14 changes: 14 additions & 0 deletions 03_packages_basics/factorial/Makefile
@@ -0,0 +1,14 @@
include $(GOROOT)/src/Make.inc

TARG=factorial
GOFMT=gofmt -spaces=true -tabindent=false -tabwidth=4

GOFILES=\
factorial.go\

include $(GOROOT)/src/Make.pkg

format:
${GOFMT} -w factorial.go
${GOFMT} -w main.go

12 changes: 12 additions & 0 deletions 03_packages_basics/factorial/factorial.go
@@ -0,0 +1,12 @@
package factorial

// Functions those start from uppercase are pulic and would be imported
// to access from other packages
func Calc( n int ) (rv int) {
if n == 0 {
rv = 1
} else {
rv = n * Calc( n - 1 )
}
return rv
}
19 changes: 19 additions & 0 deletions 03_packages_basics/factorial/factorial_test.go
@@ -0,0 +1,19 @@
package factorial_test

import (
"testing";
"factorial";
)

func TestCalc(t *testing.T){
if factorial.Calc( 5 ) != 120 {
t.Errorf("Error calculation factorial\nExpected 120 was %d\n", factorial.Calc( 5 ) )
}
if factorial.Calc( 0 ) != 1 {
t.Errorf("Error calculation factorial\nExpected 1 was %d\n", factorial.Calc( 0 ) )
}
if factorial.Calc( 1 ) != 1 {
t.Errorf("Error calculation factorial\nExpected 1 was %d\n", factorial.Calc( 1 ) )
}
}

2 changes: 1 addition & 1 deletion 03_packages_basics/main.go
@@ -1,7 +1,7 @@
package main

import formated_output "fmt"
import "./factorial"
import "factorial"

func main() {
formated_output.Printf( "Packeges samples \n");
Expand Down
2 changes: 1 addition & 1 deletion 04_arrays/Makefile
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

TARG=arrays
GOFILES=arrays.go
Expand Down
2 changes: 1 addition & 1 deletion 05_slices/Makefile
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

TARG=slices
GOFILES=slices.go
Expand Down
2 changes: 1 addition & 1 deletion 06_maps/Makefile
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

TARG=maps
GOFILES=maps.go
Expand Down
17 changes: 4 additions & 13 deletions 07_structs/Makefile
@@ -1,16 +1,7 @@
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.inc

.SUFFIXES: .go .$O

OBJS=super_point.$O structs.$O
TARG=structs

structs: $(OBJS)
$(LD) -o structs $(OBJS)

clean:
rm -f $(OBJS) structs

.go.$O:
$(GC) $<

GOFILES=structs.go super_point.go

include $(GOROOT)/src/Make.cmd
3 changes: 1 addition & 2 deletions 07_structs/structs.go
Expand Up @@ -2,7 +2,6 @@
package main

import fmt "fmt"
import super_point "./super_point"

// struct type declaretion
type Point struct { x, y float };
Expand Down Expand Up @@ -59,7 +58,7 @@ func main( ){

// exporting structs from other packages
// struct fields visibility
var spoint super_point.SuperPoint;
var spoint SuperPoint;
spoint.X = 10;
spoint.Y = 15;
fmt.Println( spoint.X, spoint.Y );
Expand Down
2 changes: 1 addition & 1 deletion 07_structs/super_point.go
@@ -1,4 +1,4 @@
package super_point
package main

// type for export should start with Uppercase
type SuperPoint struct {
Expand Down
11 changes: 11 additions & 0 deletions 10_http/Makefile
@@ -0,0 +1,11 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

include $(GOROOT)/src/Make.inc

TARG=10_http
GOFILES=\
http.go\

include $(GOROOT)/src/Make.cmd
57 changes: 57 additions & 0 deletions 10_http/http.go
@@ -0,0 +1,57 @@
package main

import (
"flag"
"http"
"io"
"log"
"template"
)

var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var fmap = template.FormatterMap{
"html": template.HTMLFormatter,
"url+html": UrlHtmlFormatter,
}
var templ = template.MustParse(templateStr, fmap)

func main() {
flag.Parse()
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Exit("ListenAndServe:", err)
}
}

func QR(w http.ResponseWriter, req *http.Request) {
templ.Execute(req.FormValue("s"), w)
}

func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
}


const templateStr = `
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
{.section @}
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={@|url+html}"
/>
<br>
{@|html}
<br>
<br>
{.end}
<form action="/" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr>
</form>
</body>
</html>
`

0 comments on commit 38f1ab3

Please sign in to comment.