From 50104cc32a498f7517a51c8dc93106c51c7a54b4 Mon Sep 17 00:00:00 2001
From: Ondřej Surý
If your change affects performance, add a
@@ -322,3 +322,47 @@ reported.
See the gotest documentation and the
testing package for more detail.
First, a disclaimer: very few Go packages should need to know about the
+hardware and operating system they run on. In the vast majority of cases the
+language and standard library handle most portability issues. This section is
+a guide for experienced systems programmers who have a good reason to write
+platform-specific code, such as assembly-language support for fast
+trigonometric functions or code that implements a common interface above
+different operating systems. To compile such code, use the For example, this The OS-specific code goes in If you follow these conventional parameterizations, tools such as
+goinstall will work seamlessly with your package:
+ The same holds for gotest one_test.go
.
Benchmark
function
(see the gotest command documentation)
-and run it using gotest -benchmarks=.
.
+and run it using gotest -test.bench=.
.
Architecture- and operating system-specific code
+
+$GOOS
and $GOARCH
+environment variables in your
+source file names and Makefile
.Makefile
describes a package that builds on
+different operating systems by parameterizing the file name with
+$GOOS
.
+include $(GOROOT)/src/Make.inc
+
+TARG=mypackage
+GOFILES=\
+ my.go\
+ my_$(GOOS).go\
+
+include $(GOROOT)/src/Make.pkg
+
+
+my_linux.go
,
+my_darwin.go
, and so on.
+my_$(GOOS).go
+my_$(GOARCH).go
+my_$(GOOS)_$(GOARCH).go
+
+
+.s
(assembly) files.
-!./srcextract.bin -src=part1.go -name=Page +!srcextract.bin -src=part1.go -name=Page
@@ -95,7 +95,7 @@ But what about persistent storage? We can address that by creating a
-!./srcextract.bin -src=part1.go -name=save +!srcextract.bin -src=part1.go -name=save
@@ -131,7 +131,7 @@ We will want to load pages, too:
-!./srcextract.bin -src=part1-noerror.go -name=loadPage +!srcextract.bin -src=part1-noerror.go -name=loadPage
@@ -155,7 +155,7 @@ function to return *Page
and os.Error
.
-!./srcextract.bin -src=part1.go -name=loadPage +!srcextract.bin -src=part1.go -name=loadPage
@@ -173,7 +173,7 @@ written:
-!./srcextract.bin -src=part1.go -name=main +!srcextract.bin -src=part1.go -name=main
@@ -211,7 +211,7 @@ Here's a full working example of a simple web server:
-!./htmlify.bin < http-sample.go +!htmlify.bin < http-sample.go
@@ -276,9 +276,9 @@ Let's create a handler to view a wiki page:
-!./srcextract.bin -src=part2.go -name=lenPath +!srcextract.bin -src=part2.go -name=lenPath -!./srcextract.bin -src=part2.go -name=viewHandler +!srcextract.bin -src=part2.go -name=viewHandler
@@ -309,7 +309,7 @@ any requests under the path /view/
.
-!./srcextract.bin -src=part2.go -name=main +!srcextract.bin -src=part2.go -name=main
@@ -348,7 +348,7 @@ First, we add them to main()
:
-!./srcextract.bin -src=final-noclosure.go -name=main +!srcextract.bin -src=final-noclosure.go -name=main
@@ -358,7 +358,7 @@ and displays an HTML form.
-!./srcextract.bin -src=notemplate.go -name=editHandler +!srcextract.bin -src=notemplate.go -name=editHandler
@@ -394,7 +394,7 @@ Open a new file named edit.html
, and add the following lines:
-!./htmlify.bin < edit.html +!htmlify.bin < edit.html
@@ -403,7 +403,7 @@ HTML:
-!./srcextract.bin -src=final-noerror.go -name=editHandler +!srcextract.bin -src=final-noerror.go -name=editHandler
@@ -438,7 +438,7 @@ While we're working with templates, let's create a template for our
-!./htmlify.bin < view.html +!htmlify.bin < view.html
@@ -446,7 +446,7 @@ Modify viewHandler
accordingly:
-!./srcextract.bin -src=final-noerror.go -name=viewHandler +!srcextract.bin -src=final-noerror.go -name=viewHandler
@@ -456,11 +456,11 @@ to its own function:
-!./srcextract.bin -src=final-template.go -name=viewHandler +!srcextract.bin -src=final-template.go -name=viewHandler -!./srcextract.bin -src=final-template.go -name=editHandler +!srcextract.bin -src=final-template.go -name=editHandler -!./srcextract.bin -src=final-template.go -name=renderTemplate +!srcextract.bin -src=final-template.go -name=renderTemplate
@@ -477,7 +477,7 @@ redirect the client to the edit Page so the content may be created:
-!./srcextract.bin -src=final-noclosure.go -name=viewHandler +!srcextract.bin -src=final-noclosure.go -name=viewHandler
@@ -493,7 +493,7 @@ The function saveHandler
will handle the form submission.
-!./srcextract.bin -src=final-template.go -name=saveHandler +!srcextract.bin -src=final-template.go -name=saveHandler
@@ -525,7 +525,7 @@ First, let's handle the errors in renderTemplate
:
-!./srcextract.bin -src=final-parsetemplate.go -name=renderTemplate +!srcextract.bin -src=final-parsetemplate.go -name=renderTemplate
@@ -539,7 +539,7 @@ Now let's fix up saveHandler
:
-!./srcextract.bin -src=final-noclosure.go -name=saveHandler +!srcextract.bin -src=final-noclosure.go -name=saveHandler
@@ -564,7 +564,7 @@ our *Template
values, keyed by string
-!./srcextract.bin -src=final.go -name=templates +!srcextract.bin -src=final.go -name=templates
@@ -577,7 +577,7 @@ be loaded the only sensible thing to do is exit the program.
-!./srcextract.bin -src=final.go -name=init +!srcextract.bin -src=final.go -name=init
@@ -593,7 +593,7 @@ the Execute
method on the appropriate Template
from
templates
:
-!./srcextract.bin -src=final.go -name=renderTemplate +!srcextract.bin -src=final.go -name=renderTemplate
-!./srcextract.bin -src=final-noclosure.go -name=titleValidator +!srcextract.bin -src=final-noclosure.go -name=titleValidator
@@ -628,7 +628,7 @@ URL, and tests it against our TitleValidator
expression:
-!./srcextract.bin -src=final-noclosure.go -name=getTitle +!srcextract.bin -src=final-noclosure.go -name=getTitle
@@ -643,11 +643,11 @@ Let's put a call to getTitle
in each of the handlers:
-!./srcextract.bin -src=final-noclosure.go -name=viewHandler +!srcextract.bin -src=final-noclosure.go -name=viewHandler -!./srcextract.bin -src=final-noclosure.go -name=editHandler +!srcextract.bin -src=final-noclosure.go -name=editHandler -!./srcextract.bin -src=final-noclosure.go -name=saveHandler +!srcextract.bin -src=final-noclosure.go -name=saveHandler
getTitle
and use it here
-!./srcextract.bin -src=final.go -name=makeHandler +!srcextract.bin -src=final.go -name=makeHandler
@@ -723,7 +723,7 @@ package:
-!./srcextract.bin -src=final.go -name=main +!srcextract.bin -src=final.go -name=main
@@ -732,11 +732,11 @@ making them much simpler:
-!./srcextract.bin -src=final.go -name=viewHandler +!srcextract.bin -src=final.go -name=viewHandler -!./srcextract.bin -src=final.go -name=editHandler +!srcextract.bin -src=final.go -name=editHandler -!./srcextract.bin -src=final.go -name=saveHandler +!srcextract.bin -src=final.go -name=saveHandler
This page summarizes the changes between tagged releases of Go. For full details, see the Mercurial change log.
--This release includes changes to the io and template packages. +This release includes changes to the reflect and path packages. +Code that uses reflect or path may need to be updated. + +The reflect package's Value.Addr method has been renamed to Value.UnsafeAddr. +Code that uses the Addr method will have to call UnsafeAddr instead. + +The path package has been split into two packages: path and path/filepath. +Package path manipulates slash-separated paths, regardless of operating system. +Package filepath implements the local operating system's native file paths. +OS-specific functioanlity in pacakge path, such as Walk, moved to filepath. + +Other changes: +* build: fixes and simplifications (thanks Dave Cheney), + move $GOBIN ahead of /bin, /usr/bin in build $PATH. +* bzip2: speed up decompression. +* cgo: fix dwarf type parsing (thanks Gustavo Niemeyer), + put temporary source files in _obj (thanks Roger Peppe), + fix bug involving 0-argument callbacks. +* compress/lzw: optimizations. +* doc: add FAQ about "implements", + add FAQ about large binaries , + add FAQ about stack vs heap allocation, + add internationalization to roadmap, + describe platform-specific conventions in code.html. +* fmt: allow recursive calls to Fscan etc (thanks Roger Peppe), + make %#p suppress leading 0x. +* gc, gopack: add some missing flags to the docs. +* gc: fix init of packages named main (thanks Gustavo Niemeyer), +* gob: make recursive map and slice types work, and other fixes. + tentative support for GobEncoder/GobDecoder interfaces. +* gobuilder: add -package flag to build external packages and -v for verbose. +* gofmt: exclude test file that is not legal Go. +* goinstall: protect against malicious filenames (thanks Roger Peppe). +* goyacc: provide -p flag to set prefix for names, documentation update. +* http: add cookie support (thanks Petar Maymounkov), + allow handlers to send non-chunked responses, + export ParseHTTPVersion, + expose Client's Transport, + use WriteProxy, + rename ClientTransport to Transport. +* http/cgi: new package. +* http/httptest: new package. +* image: add a decoding test for common file formats. +* io/ioutil: add TempDir. +* mime/multipart: Header changed from map to MIMEHeader +* path/filepath: new OS-specific path support (thanks Gustavo Niemeyer). +* reflect: add PtrTo, add Value.Addr (old Addr is now UnsafeAddr). +* runtime: use kernel-supplied compare-and-swap on linux/arm. +* spec: minor clarification of scope rule for functions. +* sync/atomic: new package to expose atomic operations. +* syscall: regenerate zerrors_freebsd_amd64.go (thanks Mikio Hara), + work around FreeBSD execve kernel bug (thanks Devon H. O'Dell). +* template: document the delimiters. +* testing: run GC before each benchmark run (thanks Roger Peppe). +* unsafe: fix the documentation. +* websocket: use httptest.Server for tests (thanks Robert Hencke). +* xml: permit nested directives (thanks Chris Dollin). ++ +
+This release includes changes to the http package and a small language change. +Your code will require changes if it manipulates http Headers or omits the +condition in if statements. + +The new http.Header type replaces map[string]string in the Header and Trailer +fields of http.Request and http.Response. +A Header value can be manipulated via its Get, Set, Add, and Del methods. +See http://golang.org/pkg/http/#Header + +The condition is now mandatory in if statements. +Previously it would default to true, as in switch and for statements. +This code is now illegal: + if x := foo(); { + // code that is always executed + } +The same effect can be achieved like this: + if x := foo(); true { + // code + } +Or, in a simpler form: + { + x := foo() + // code + } + +Other changes: +* 6l: new -Hwindowsgui flag allows to build windows gui pe (thanks Alex Brainman), + pe fixes (thanks Wei Guangjing). +* 8l, 6l: allow for more os threads to be created on Windows (thanks Alex Brainman), +* build: reduce the use of subshells in recursive make, and + remove unused NaCl conditional from make.bash (thanks Dave Cheney). +* codereview: fix clpatch with empty diffs (thanks Gustavo Niemeyer). +* compress/bzip2: add package. +* compress/lzw: implement a decoder. +* crypto/openpgp: add package. +* crypto/rand: add read buffer to speed up small requests (thanks Albert Strasheim). +* crypto/rsa: left-pad OAEP results when needed. +* crypto/tls: make protocol negotiation failure fatal. +* fmt: stop giving characters to the Scan method of Scanner when we hit a newline in Scanln. +* gc: interface error message fixes, + make string const comparison unsigned (thanks Jeff R. Allen). +* go spec: minor clarification on channel types. +* go/ast, parser: condition in if statement is mandatory. +* gob: compute information about a user's type once. + protect against pure recursive types. +* godoc: accept symbolic links as path names provided to -path, + add robots.txt, log errors when reading filter files. +* html: tokenize HTML comments. +* http: add proxy support (thanks Yasuhiro Matsumoto), + implement with net/textproto (thanks Petar Maymounkov), + send full URL in proxy requests, + introduce start of Client and ClientTransport. +* image/png: support for more formats (thanks Mikael Tillenius). +* json: only use alphanumeric tags, + use base64 to encode []byte (thanks Roger Peppe). +* ld: detect stack overflow due to NOSPLIT, drop rpath, support weak symbols. +* misc/dashboard/builder: talk to hg with utf-8 encoding. +* misc/dashboard: notify golang-dev on build failure. +* net: *netFD.Read to return os.EOF on eof under windows (thanks Alex Brainman), + add IPv4 multicast to UDPConn (thanks Dave Cheney), + more accurate IPv4-in-IPv6 API test (thanks Mikio Hara), + reject invalid net:proto network names (thanks Olivier Antoine). +* netchan: allow use of arbitrary connections (thanks Roger Peppe). +* os: add ENODATA and ENOTCONN (thanks Albert Strasheim). +* reflect: add a couple of sentences explaining how Methods operate, + add a secret method to ArrayOrSliceType to ensure it’s only implemented by arrays and slices, + add pointer word to CommonType (placeholder for future work). +* runtime-gdb.py: gdb pretty printer for go strings properly handles length. +* runtime: various bug fixes, more complete stack traces, + record $GOROOT_FINAL for runtime.GOROOT. +* spec: delete incorrect mention of selector working on pointer to interface type. +* sync: add Cond (thanks Gustavo Niemeyer). +* syscall: add MCL_* flags for mlockall (thanks Albert Strasheim), + implement chmod() for win32 (thanks Yasuhiro Matsumoto). +* test/bench: update timings for new GC. +* testing: rename cmdline flags to avoid conflicts (thanks Gustavo Niemeyer). +* textproto: introduce Header type (thanks Petar Maymounkov). +* websocket: use new interface to access Header. ++ +
+This release includes changes to the io, os, and template packages. You may need to update your code. The io.ReadByter and io.ReadRuner interface types have been renamed to io.ByteReader and io.RuneReader respectively. +The os package's ForkExec function has been superseded by the new StartProcess +function and an API built around the Process type: + http://golang.org/pkg/os/#Process + The order of arguments to template.Execute has been reversed to be consistent the notion of "destination first", as with io.Copy, fmt.Fprint, and others. diff --git a/doc/devel/roadmap.html b/doc/devel/roadmap.html index 9a3c4eaba..97d8a08b8 100644 --- a/doc/devel/roadmap.html +++ b/doc/devel/roadmap.html @@ -47,6 +47,8 @@ App Engine support. Improved CGO including some mechanism for calling back from C to Go.
godoc
, like gofmt
,
takes care of that.
-Finally, the comments are uninterpreted plain text, so HTML and other
+The comments are uninterpreted plain text, so HTML and other
annotations such as _this_
will reproduce verbatim and should
not be used.
+Depending on the context, godoc
might not even
+reformat comments, so make sure they look good straight up:
+use correct spelling, punctuation, and sentence structure,
+fold long lines, and so on.
diff --git a/doc/gccgo_contribute.html b/doc/gccgo_contribute.html
index cab6967f3..8eeb3a5c5 100644
--- a/doc/gccgo_contribute.html
+++ b/doc/gccgo_contribute.html
@@ -45,7 +45,7 @@ a gcc-interface
subdirectory.
-The runtime library for gccgo
is mostly the same as the
+The run-time library for gccgo
is mostly the same as the
library in the main Go
repository. The library code in the Go repository is periodically
copied into the gofrontend
and the gcc
diff --git a/doc/gccgo_install.html b/doc/gccgo_install.html
index 2ab6dcdae..159fab7bb 100644
--- a/doc/gccgo_install.html
+++ b/doc/gccgo_install.html
@@ -116,7 +116,7 @@ gccgo -o file file.o
To run the resulting file, you will need to tell the program where to
-find the Go runtime library. This can be done either by setting
+find the compiled Go packages. This can be done either by setting
LD_LIBRARY_PATH
in your environment:
diff --git a/doc/go_faq.html b/doc/go_faq.html index 3f9c1d246..4be811068 100644 --- a/doc/go_faq.html +++ b/doc/go_faq.html @@ -508,6 +508,71 @@ Regarding operator overloading, it seems more a convenience than an absolute requirement. Again, things are simpler without it. ++Why doesn't Go have "implements" declarations?
+ ++A Go type satisfies an interface by implementing the methods of that interface, +nothing more. This property allows interfaces to be defined and used without +having to modify existing code. It enables a kind of "duck typing" that +promotes separation of concerns and improves code re-use, and makes it easier +to build on patterns that emerge as the code develops. +The semantics of interfaces is one of the main reasons for Go's nimble, +lightweight feel. +
+ ++See the question on type inheritance for more detail. +
+ ++How can I guarantee my type satisfies an interface?
+ ++You can ask the compiler to check that the type
+ +T
implements the +interfaceI
by attempting an assignment: ++type T struct{} +var _ I = T{} ++ ++If
+ +T
doesn't implementI
, the mistake will be caught +at compile time. ++If you wish the users of an interface to explicitly declare that they implement +it, you can add a method with a descriptive name to the interface's method set. +For example: +
+ ++type Fooer interface { + Foo() + ImplementsFooer() +} ++ ++A type must then implement the
+ +ImplementsFooer
method to be a +Fooer
, clearly documenting the fact. ++type Bar struct{} +func (b Bar) ImplementsFooer() {} +func (b Bar) Foo() {} ++ ++Most code doesn't make use of such constraints, since they limit the utility of +the interface idea. Sometimes, though, they're necessary to resolve ambiguities +among similar interfaces. +
+Values
@@ -677,6 +742,28 @@ floating-point numbers. The default size of a floating-point constant isfloat64
. ++How do I know whether a variable is allocated on the heap or the stack?
+ ++From a correctness standpoint, you don't need to know. +Each variable in Go exists as long as there are references to it. +The storage location chosen by the implementation is irrelevant to the +semantics of the language. + +
+The storage location does have an effect on writing efficient programs. +When possible, the Go compilers will allocate variables that are +local to a function in that function's stack frame. However, if the +compiler cannot prove that the variable is not referenced after the +function returns, then the compiler must allocate the variable on the +garbage-collected heap to avoid dangling pointer errors. + +
+In the current compilers, the analysis is crude: if a variable has its address +taken, that variable is allocated on the heap. We are working to improve this +analysis so that more data is kept on the stack. +
Concurrency
@@ -708,7 +795,7 @@ Why doesn't my multi-goroutine program use multiple CPUs?
Under the gc compilers you must set
@@ -716,7 +803,7 @@ effectively equal to the number of running goroutines.GOMAXPROCS
to allow the -runtime to utilise more than one OS thread. Undergccgo
an OS +run-time support to utilise more than one OS thread. Undergccgo
an OS thread will be created for each goroutine, andGOMAXPROCS
is effectively equal to the number of running goroutines.Programs that perform concurrent computation should benefit from an increase in
@@ -737,8 +824,8 @@ penalty involved in sending data between threads.GOMAXPROCS
. (See the runtime package +href="http://golang.org/pkg/runtime/#GOMAXPROCS">runtime
package's documentation.)-The Go runtime's scheduler is not as good as it needs to be. In future, it -should recognise such cases and optimize its use of OS threads. For now, +Go's goroutine scheduler is not as good as it needs to be. In future, it +should recognize such cases and optimize its use of OS threads. For now,
@@ -920,13 +1007,13 @@ parser are already available inGOMAXPROCS
should be set on a per-application basis./pkg/go
.) We also considered using LLVM for6g
but we felt it was too large and slow to meet our performance goals. --How is the runtime implemented?
++How is the run-time support implemented?
-Again due to bootstrapping issues, the runtime is mostly in C (with a +Again due to bootstrapping issues, the run-time code is mostly in C (with a tiny bit of assembler) although Go is capable of implementing most of -it now.
Gccgo
's runtime usesglibc
. +it now.Gccgo
's run-time support usesglibc
.Gc
uses a custom library, to keep the footprint under control; it is compiled with a version of the Plan 9 C compiler that supports @@ -934,6 +1021,21 @@ segmented stacks for goroutines. Work is underway to provide the same stack management ingccgo
. ++Why is my trivial program such a large binary?
+ ++The gc tool chain (
5l
,6l
, and8l
) only +generate statically linked binaries. All Go binaries therefore include the Go +run-time, along with the run-time type information necessary to support dynamic +type checks, reflection, and even panic-time stack traces. + ++A trivial C "hello, world" program compiled and linked statically using gcc +on Linux is around 750 kB. An equivalent Go program is around 1.8 MB, but +that includes more powerful run-time support. We believe that with some effort +the size of Go binaries can be reduced. +
Performance
diff --git a/doc/go_for_cpp_programmers.html b/doc/go_for_cpp_programmers.html index 608ab147b..7168f1d05 100644 --- a/doc/go_for_cpp_programmers.html +++ b/doc/go_for_cpp_programmers.html @@ -555,7 +555,7 @@ When you want the equivalent of a virtual function, use an interface. A variable which has an interface type may be converted to have a different interface type using a special construct called a type assertion. This is implemented dynamically -at runtime, like C++
dynamic_cast
. Unlike +at run time, like C++dynamic_cast
. Unlikedynamic_cast
, there does not need to be any declared relationship between the two interfaces. @@ -589,7 +589,7 @@ must unbox using a type assertion to recover values of the contained type. As the typing is dynamic rather than static, there is no equivalent of the way that a C++ template may inline the relevant operations. The operations are fully type-checked -at runtime, but all operations will involve a function call. +at run time, but all operations will involve a function call.type iterator interface { diff --git a/doc/go_spec.html b/doc/go_spec.html index a95ed704a..85dfc44bd 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ - +