create a file and write nothing. try compiling as followsprogram1.go
cmd-prompt>
go run program1.go
an error similar to the following will be printed:
package :
program1.go:1:1: expected 'package', found 'EOF'
in a language, all files must belong to a package. currently, you only need to understand that you can declare one at the head of the file, where it is a keyword, a package name for yourself.
in large programs, packages can be well organized for various functions.
for example, if you want to write a virtual model of a vehicle, you should put all the models that belong into a package called a package, and put all the models that belong into the package.
organization-related features are just one use for packages, and more will be covered in a follow-up article.Go
package name
package
name
car
cars
bus
buses
now let’s add a statement to the file we just created and then re-execute the command
program1.go content
package main
after executing the previous command, the following error is printed:
runtime.main: undefined: main.main
Go
when the program starts, it needs to have an identifiable entry in the file. just as a car must have a key to start the ignition, a computer needs to have a power-on button, and a program needs to have a function.Go
main
add another line to the file and try again
program1.go content
package main
func main() {}
execute the commandgo run program1.go
the program executed correctly, but since we didn’t do anything else, the program exited quickly.
congratulations, so far we have created our first program. although it is not useful, it can already work normally.
let’s go ahead and add a line
program1.go content
package main
func main() {
Println("Hello world")
}
if you try to run, you will print the following error
program1.go:4: undefined: Println
Println
is an input to the screen. After executing the command, the compiler reports that it is not defined. Why? Remember the packages mentioned earlier? Yeah, here we need to use the bag. Functions like this are stored in some packages. However, currently these packages are not actively introduced due to us, but can not use drops. If we need to use the features in these packages, we need them first. It’s like when we buy a car from overseas. Ok, let’s try.Println
import
import
The function Println and other functions that read and write text and characters are stored in an abbreviation called a packagefmt
formatting
The Go programming language follows a short and concise manner. If you write Java code, you define it in a long naming style. For example, the above will be very normal to be called. But in the Go language, it is necessary to break this routine and pursue simplicity. At first, I didn’t get used to this pattern, but after using it for a while, Zhen nyima was amazing. The code has become cleaner, it’s faster to read, and oddly enough, it’s not reduced in readability. Personal opinion Oh ~~~
formatting package
formatting
so far, let’s add a few lines of code
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
run the program with the following output:go run program1.go
Hello world
Wow, isn’t it amazing, we just added a statement below and the first Go program is already working. After that, it can be called by way. Got it, it’s that simple.package
import
import
Println
包名.
Golang is a magical language that lets us progress together