[Golang] Deep into Hello World!

Let's start by understanding the hello.go example

package main

import (
	"fmt"

	"golang.org/x/example/stringutil"
)

func main() {
	fmt.Println(stringutil.Reverse("!selpmaxe oG ,olleH"))
}

hello.go include three parts: packages, import and func main().

packages

The first statement of every go source file must be a package declaration.
Package is a way for collecting related Go code together.
A packages can have many files inside of it. For example:

main.go

// main.go
package main
import (
	"fmt"
)

func main() {
	fmt.Println("Hello MAIN!")
}

help.go

// help.go
package main
import (
	"fmt"
)

func help() {
	fmt.Println("Help!")
}

There are two type of packages

  • Executable: Generate a file that we can run.
  • Reuseable: Code dependencies or libraries that we can reuse.

Use package name main to specify this package can be compiled and then executed. Inside the main package, it must has a func call 'main'

Other package name defines a package that cab be used as a dependency.
We will discuss how to use reuseable packages later.

import

Use to import code from other packages.
Although there are some standard librariessuch as math, fmt ,debug ... etc.
We still need to use import to link the library to our package.
you can find out more standard libraries on golang.org/pkg

Besides, you can import third party packages from internet as well.
for example import "golang.org/x/example/stringutil"
This required you to install the package via command go get <package> before building your own package.

func main()

The entry of our execuable code. This function is required for main package.


<<:  AI平台初学者工作坊: 从training、tracking到serving

>>:  DAY2-必先利其器

【11】二分类问题下 Binary Cross Entropy 的使用注意事项

Colab连结 接着昨天讨论到的 Cross Entropy ,今天把重点放到了 BinaryCro...

自动化 End-End 测试 Nightwatch.js 之踩雷笔记:点击物件 III

点击物件是蛮基本的操作,不过还是有很多地方需要注意。 回顾 第一天提到了如果该物件是 div,例如这...

关於除错这件事

发达的工具会剥夺人的能力,能力被剥夺後经验会开始狭隘,狭隘的经验则会让思维开始产生死角,有死角的思维...

Youtube Data API 教学 - 告一个段落

「鲑鱼均,因为一场鲑鱼之乱被主管称为鲑鱼世代,广义来说以年龄和脸蛋分类的话这应该算是一种 KNN 的...

[Day 3] Leetcode 848. Shifting Letters (C++)

前言 今天的题目在这里: 848. Shifting Letters,被归类为medium,乍看之下...