go Language Learning (C): Source file

go command language source code files include source files, libraries, source files and test source code files.

       Source files is the command entry program executed, belonging to the main package, containing no free parameters main function returns a result, with similar java, the same package is not recommended to store a plurality of the following main functions. Command source files to generate an executable file compiled or installed, the presence of the current directory execute the command.

        Library source files used to store other programs for use of the entity code, a build checked and verified, generate temporary files, archive files generated during installation, store pkg directory in the current workspace.

        The main test source code file stored test the relevant source documents, including functional testing, performance testing, and so run the sample program.

The following talk about the details command source files and library source files:

1. Command source file

1) Code 1

As the code, we perform operations run in the IDE, or the command line go run Demo1.go

2) how the source file command accepts parameters? fla go language code package dedicated to receiving and parsing the command line parameters, such as the following codes:

package main

import (
	"flag"
	"fmt"
)

var name string

func init(){
	flag.StringVar(&name, "name", "everyone", "The greeting object.")
}
func main() {
	flag.Parse()
	fmt.Printf("hollo, %s!\n", name)
}


The above code, flag.StringVar there are four parameters, namely command line arguments address, command line parameter name, the default value of an output parameter description, it must be resolved before the command line arguments, as described above flag.parse

3) customizable parameters commands the source files for use

package main

import (
	"flag"
	"fmt"
	"os"
)

var name string

func init(){
	flag.StringVar(&name, "name", "everyone", "The greeting object.")
}
func main() {
	flag.CommandLine.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", "question")
		flag.PrintDefaults()
	}
	flag.Parse()
	fmt.Printf("hollo, %s!\n", name)
}


The above type in which PanicOnError, a total of three kinds of error types, see below Source Definition:

const (
  ContinueOnError ErrorHandling = iota // Return a descriptive error.
  ExitOnError                          // Call os.Exit(2).
  PanicOnError                         // Call panic with a descriptive error.
)

Execute commands and results shown below:

4) a custom command parameter container can be more flexibly define the command line parameters

package main

import (
	"flag"
	"fmt"
	"os"
)

var name string

var cmdLine = flag.NewFlagSet("question", flag.ExitOnError)
func init(){
	cmdLine.StringVar(&name, "name", "everyone", "The greeting object.")
}
func main() {
	cmdLine.Parse(os.Args[1:])
	fmt.Printf("hollo, %s!\n", name)
}


2. Treasury source file

Treasury source file is a file can not be run directly, can only be used by other programs. go language, if the current file you want to use the function declaration in a file, you need to follow the following files in the same package.

See below 2 of the code:

//Demo6.go

package main
import "fmt"
//定义方法
func printHello(name string) {
  fmt.Printf("Hello, %s!\n", name)
}
//Demo5.go
package main

import (
  "flag"
  "os"
)

var name string

var cmdLine = flag.NewFlagSet("question", flag.ExitOnError)
func init(){
  cmdLine.StringVar(&name, "name", "everyone", "The greeting object.")
}
func main() {
  cmdLine.Parse(os.Args[1:])
  printHello(name)
}

Run the following command:

Note that the above command is run Demo5 and Demo6. Another way is to put two files into a package, the implementation of the compiler. Directory structure is as follows:

In the command line go build demo1, will produce demo1.exe file, and then run the executable file, output: "! Hello, everyone", as shown below:

   

note:

a) mygolang working directory must be configured to GOPATH environment variable, otherwise it will be reported next map error;

b) following the same package source files to declare the same code package. If the package below command source files, that other types of source files should be declared as your main pack.

c) a statement of source code package files can not the same directory, but if you build a code package, resulting in the same package with the code name. As examples of the above

Configuring environment variables:

d) If the modified package directory structure, as shown below:

//Demo6.go
package lib

import "fmt"

//定义方法
func PrintHello(name string) {
  fmt.Printf("Print Hello, %s!\n", name)
}
//Demo5.go
package main

import (
  "demo2/lib"
  "flag"
  "os"
)

var name string

var cmdLine = flag.NewFlagSet("question", flag.ExitOnError)
func init(){
  cmdLine.StringVar(&name, "name", "everyone", "The greeting object.")
}
func main() {
  cmdLine.Parse(os.Args[1:])
  lib.PrintHello(name)
}

In this case, Demo5.go to rely Demo6.go file, you need to install about lib package, go install demo2 \ lib

This time it will generate an archive file, then compile and run

e) in the above program Demo6.go entities to the capital so that it can be referenced from outside the package, if it is lower case, the program can be referenced in the packet.

f) go different language support package statement and directory, but in order to make the code clearer and less misunderstanding, it is recommended that two names remain the same

g) a statement of internal code package in a module, you can only make use of the current code package, including direct parent package and the child package internal code package, other packages do not support code module references.

 

Code address: https: //github.com/jinjunzhu/mygolang.git

Published 33 original articles · won praise 2 · views 40000 +

Guess you like

Origin blog.csdn.net/zjj2006/article/details/104925763
Recommended