Getting started with Go language && Create the first Go program based on GoLand 2023.1

Getting started with the Go language

What is the go language?

The Go language (also known as Golang) is an open-source programming language developed by Google and debuted in 2009. It is a statically typed, compiled language with automatic garbage collection and powerful concurrent programming support.

The Go language is designed to be concise, easy to learn and use, and to be efficient, reliable and extensible. Its syntax style is similar to C language, but removes many complex features in C language, such as pointers and memory management.

Go language can be used to write various types of applications, including server-side applications, network applications, cloud platform applications, operating systems and system tools, etc. It is also widely used in the internal development of large Internet companies and other organizations, such as Google, Uber, Dropbox, and Docker, etc.

What do I need to learn to get started with go?

  1. Go language basics: including language basics, variables, data types, control statements, functions, pointers, arrays, slices, maps, structures, etc.

  2. Concurrency in Go language: Go language naturally supports concurrency. To learn concurrent programming, you need to master the use of goroutine and channel, as well as common concurrency models and concurrent programming skills.

  3. Go language standard library: The standard library of Go language is very rich, and learning how to use the standard library can allow you to write efficient programs more quickly.

  4. Go language development tools and environment: Learn how to install and configure the Go language development environment, and how to use Go's command line tools to compile, run, and debug programs.

  5. Go language network programming: Go language naturally supports network programming, learn how to use Go to write network programs, including programming of protocols such as HTTP, TCP, and UDP.

  6. Go language testing and debugging: Go language has a built-in testing framework. Learning how to write test cases and how to use debugging tools for debugging can help you better develop and maintain Go language programs.

What are the advantages of the Go language?

1. Efficiency: The compiler and runtime system of the Go language are optimized to be very efficient, and can achieve excellence in performance and resource utilization efficiency. In addition, the concurrency model of the Go language is also designed to be very efficient and capable of supporting large-scale concurrency.

  1. Concise and easy to learn: The syntax of the Go language is concise and clear, the language specification is clear, and the learning curve is relatively smooth. Compared with other languages, the syntax of Go language is more concise and easy to read, without redundant syntax and complicated features.

  2. Concurrent programming: Go language naturally supports concurrent programming and provides primitives such as goroutine and channel, making it easy to write efficient and concise concurrent programs. Concurrent programming is also a highlight and advantage of the Go language, which is widely used in network programming and high-concurrency scenarios.

  3. Cross-platform: Go language compilers can generate local codes on different platforms, support cross-platform development, and can write and run the same program on different operating systems and hardware.

  4. Development efficiency: The Go language provides a complete set of standard libraries and tool chains, which can help developers improve development efficiency. At the same time, the static type and automatic garbage collection of the Go language can reduce the possibility of code errors and improve the stability and maintainability of the code.

  5. Active community: The Go language community is highly active, with a huge developer community and numerous open source projects, which can provide rich resources and technical support for easy learning and application.

Comparison of Go language and Java language

  1. Grammatical style: There are differences in the grammatical style between the Go language and the Java language. The Go language focuses on simplicity and readability, and avoids excessive grammar and keywords; while the grammar of the Java language is more complicated and requires a large number of keywords and grammatical structures. Realize various functions.

  2. Concurrency model: There are differences in the concurrency model between the Go language and the Java language. The Go language uses goroutines and channels to achieve efficient concurrent programming; while the Java language uses mechanisms such as threads and locks, which require developers to manage threads and locks by themselves, which is prone to problems such as deadlocks.

  3. Memory management: Go language and Java language have different memory management. The Go language adopts an automatic garbage collection mechanism, which can automatically manage memory and reduce memory leaks and other problems; while the Java language also supports a garbage collection mechanism, but due to the different implementation methods and algorithms of the JVM, there may be some differences in performance and efficiency.

  4. Performance and efficiency: Go language and Java language have certain differences in performance and efficiency. Since the Go language is designed to be efficient and concise, it has advantages in performance and efficiency, especially in large-scale concurrency and network programming. The Java language pays more attention to development efficiency and stability, so in some scenarios, the performance and efficiency are not as good as the Go language.

  5. Libraries and frameworks: The Java language has a huge ecosystem and rich third-party libraries and frameworks, which can quickly develop various applications; while the Go language ecosystem is relatively small, but there are also some excellent third-party libraries and frameworks to choose from .

In general, the Go language and the Java language have similarities and differences in some aspects. The choice of which language to use needs to be considered according to specific application scenarios and needs.

Comparison between Go language and Python language

  1. Grammatical style: There are differences in the grammatical style between the Go language and the Python language. The Go language pays attention to conciseness and readability, and avoids excessive grammar and keywords; while the Python language pays more attention to the readability and simplicity of the code. Express code blocks in a progressive way, avoiding the use of a large number of brackets and semicolons.

  2. Concurrency model: There are differences in the concurrency model between the Go language and the Python language. The Go language uses goroutine and channel to achieve efficient concurrent programming; while the Python language uses mechanisms such as multithreading and coroutines to achieve concurrent programming.

  3. Memory management: Go language and Python language have different memory management. The Go language adopts an automatic garbage collection mechanism, which can automatically manage memory and reduce memory leaks and other problems; while the Python language also supports a garbage collection mechanism, but due to its different implementation methods and algorithms, there may be some differences in performance and efficiency.

  4. Performance and efficiency: Go language and Python language have certain differences in performance and efficiency. Since the Go language is designed to be efficient and concise, it has advantages in performance and efficiency, especially in large-scale concurrency and network programming. The Python language pays more attention to development efficiency and flexibility, so in some scenarios, the performance and efficiency are not as good as the Go language.

  5. Libraries and frameworks: The Python language has a huge ecosystem and rich third-party libraries and frameworks, which can quickly develop various applications; while the Go language ecosystem is relatively small, but there are also some excellent third-party libraries and frameworks to choose from .

In general, the Go language and the Python language have similarities and differences in some aspects. The choice of which language to use needs to be considered according to specific application scenarios and needs.

Go language creates the first program based on GoLand

1. Open GoLand

insert image description here

2. Select new

insert image description here

3. Select the SDK version

insert image description hereinsert image description here
insert image description here

4. Enter the project name and click cteate

insert image description here

Project created

insert image description here

Next, create a Go file

insert image description here
Select the app file, which is the file with the main function

insert image description hereinsert image description here

You can directly enter the print function


package main

import "time"

func main() {
    
    
	println("libin9iOak !")
	println(time.DateTime)
	println(time.Now().Format("2006-01-02 15:04:05"))
	println("Hello Go Language !")
	println("I Can Do This !")
}


Right click to run;

insert image description here

operation result:
insert image description here

println(time.Now().Format("2006-01-02 15:04:05"))Statement explanation:

In Go, the rule for date formatting strings is to use some special placeholders for the different date and time components. Among them, 2006 is a special year, which is the year when the initial version of the Go language was released. Therefore, the year is used as a default placeholder for the year.

In the date format string, use 2006 for the year, 1 for the month, 2 for the day, 15 for the hour, 04 for the minute, and 05 for the second. This rule helps programmers to more easily remember the placeholders for date formatting strings.

So, when you use the statement time.Now().Format("2006-01-02 15:04:05"), it means to format the current time according to the specified format, where 2006-01 -02 15:04:05 is a date format string. Use 2006 for the year, 01 for the month, 02 for the day, 15 for the hour, 04 for the minute, and 05 for the second.

Therefore, in Go, you must write 2006 to represent the year, otherwise the Go compiler will treat it as a normal year, resulting in errors in date formatting.

That’s all for today’s sharing, see you next time~

epilogue

If this article is helpful or inspiring to you, please click three times: like, comment, bookmark ➕ follow , your support is my biggest motivation to keep writing.

Guess you like

Origin blog.csdn.net/qq_44866828/article/details/130102301