Skip to content

Latest commit

 

History

History
48 lines (29 loc) · 3.09 KB

File metadata and controls

48 lines (29 loc) · 3.09 KB

#2.1 Hello, Go Before we start building an application in Go, we need to learn how to write a simple program. It's like you cannot build a building without knowing how to build its foundation. Therefore, we are going to learn the basic syntax to run some simple programs in this section.

##Program According to international practice, before you learn how to programming in some languages, you may want to know how to write a program to print "Hello world".

Are you ready? Let's Go!

package main

import "fmt"

func main() {
	fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
}

It prints following information.

Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界

##Explanation One thing that you should know in the first is that Go programs are composed by package.

package<pkgName> (In this case is package main) tells us this source file belongs to main package, and the keyword main tells us this package will be compiled to a program instead of package files whose extensions are .a.

Every executable program has one and only one main package, and you need an entry function called main without any argument and return value in the main package.

In order to print Hello, world…, we called a function called Printf. This function is coming from fmt package, so we import this package in the third line of source code, which is import "fmt"

The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will make our own packages later.

On the fifth line, we use the keyword func to define the main function. The body of the function is inside of {}, just like C, C++ and Java.

As you can see, there are no arguments. We will learn how to write functions with arguments in just a second, and you can also have functions that have no return value or have several return values.

On the sixth line, we called the function Printf which is from the package fmt. This was called by the syntax <pkgName>.<funcName>, which is very like Python-style.

As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the <pkgName> comes from the name in package <pkgName>, not the folder's name.

You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.

##Conclusion Go uses package (like modules in Python) to organize programs. The function main.main() (this function must be in the main package) is the entry point of any program. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go supports multi-language from the time it was born.

##Links