Dienstag, 17. November 2009

go pakcages

package is a collection of source files begining with "package nameofpackage".


import "fmt"
// now useable fmt outputs
fmt.Print(...)

import my "fmt" // with my own name. (for purpose if namespace coliders ;)
my.Print(...)


each package can have an init() for initalization data (single threaded).

let see:
a.go

package a

import "fmt"

var A int

func init() {
A = 4;
fmt.Print("A init\n");
}


b.go

package b

import "fmt"

var B int

func init() {
B=15;
fmt.Print("B init\n");
}


mainab.go

package main

import (
"fmt";
"./a";
"./b";
)

func main() {
fmt.Print("call main()\n");
var x= a.A;
var y= b.B;
var z = x + y;
fmt.Print(z);
}


output:

./6.out
B init
A init
call main()


at first there will be called init. later the main.

"To build a program, the packages, and the files within them, must be compiled in the correct order.
Package dependencies determine the order in which to build packages. "

Keine Kommentare:

Kommentar veröffentlichen