This is a creation in Article, where the information may have evolved or changed.
Notes:
1 . Any one code file belongs to a package 2 . Import keyword, referencing other packages: import ("FMT") Import ("OS") is often used to write: import ( "FMT" "OS")3. The Golang executable, package main, and has only one main entry function 4. The function call in the packet: A. function in the same package, call B. Functions in different packages, by package name + dot + function name to call 5. Package access control rules: uppercase means that the function / variable is an exportable lowercase meaning that the function / variable is private, Package external cannot access
Practice:
package main /* > The result is: 0+10=101+9=102+8=103+7=104+6=105+5=106+4=107+3=108+2=109+1=1010+0=10 */ import " fmt " func list (n int for I: = 0 ; I <= N; I++ {FMT. Printf ( %d+%d=%d\n ", I, N-i, N)}}func main () {list ( 10
2. A program contains two packages of add and main, where the add package has two variables: name and age. How do I access the name and age in the main
package?
package mainimport ( // fmt " a " ) func Main () {FMT. Println ( name=: " , A.name) // add.age is a lowercase private variable, so the FMT is not found. Println ( age=: "
Main.go
Package addimport ( //_ "go_dev/day2/example2/test") func init () { " HelloWorld " Ten} var string " xxxxx " var int the
View Code
3. Application of package aliases, developing a program that uses package aliases to access the functions in the package?
Answer with 2 questions
4. Each source file can contain an init function, which is automatically called by the Go Run framework. Develop a program
Demonstrate this feature?
Directory structure
Package Mainimport (//usage of the package alias a "FMT"a"Go_dev/day2/example2/add") Func main () {//5 Print in the AddFmt. Println ("name=:", A.name)//Add.age is a lowercase private variable, so it cannot be found//6Fmt. Println ("age=:", A.age)}
Main.go
Package Testimport"FMT"varNamestring="This is the test package"varAgeint= +func init () {//1Fmt. Println ("This is a test,init") //2Fmt. Println ("test.package.name=", Name)//3Fmt. Println ("test.package.age=", age) Age=Ten //4Fmt. Println ("test.package.age=", age)}
Test.go
Package AddImport ( _"go_dev/day2/example2/test") func init () { "helloWorld" var String"xxxxx"varint
Add.go
6. Define two constants man=1 and female=2, get the number of seconds in the current time, and if they can be divisible by female, then
Print female on the terminal, otherwise print man.
Second: = time. Now (). Unix ()
Package Mainimport ("FMT" " Time")Const(Mans= Iota +1Female)/*6. Define two constants man=1 and female=2, get the number of seconds in the current time, if divisible by female, print the female at the terminal, otherwise print man. Second: = time. Now (). Unix ()*/Func Main () {//For is a dead loop . for{time. Sleep (100000*Time . microsecond) Second:=Time . Now (). Unix ()ifSecond%female = =0{fmt. Println ("female") } Else{fmt. Println ("Mans") } }}
Main.go