search
HomeheadlinesHello, I am your little cute 'GO Language'
Hello, I am your little cute 'GO Language'Mar 13, 2018 am 10:10 AM
go languagelanguage

Hello, I am your little cute GO Language

This column comprehensively interprets relevant knowledge in the software field, with a bias towards technical in-depth content. It mainly covers programming languages, system architecture, open source frameworks, technical management, etc. It is divided into multiple topics, each of which A topic contains multiple articles.

This article is the first article in the column, and also the first article in the GO language series. Today I would like to share my general impression of the GO language from all aspects. Subsequent articles will introduce in depth various features and programming techniques. .

Introduction

Historically, the authors of the Go language are Robert Griesemer, Rob Pike and Ken Thompson. Among them, Ken Thompson is known to programmers for his huge contributions to UNIX and C language development. Familiar. What software is written in Go so far? Container software Docker, basic software ETCD and Kubernetes, database software TiDB and InfluxDB, messaging system NSQ, and caching component GroupCache.

It can be seen that in almost every field of infrastructure software, new software written in Go language has emerged, and the market share of these software continues to rise. In addition to being a language for infrastructure software, Go language has more and more opportunities as a universal server-side language. Some development trends can also be seen from the popularity of Go language web frameworks such as Beego and Gorilla.

Sample program

Let’s take a look at GO’s coding style through a simple sample program:

  1. Package main

  2. import "fmt"

  3. func main(){

  4. ## fmt.Println("hello,world");

  5. }

How to run the above code? GO language is a compiled language, and the GO tool chain converts the source files of the program into machine-related Native instructions (binary), the most basic tool is the run command, which can compile and link one or more GO source files (with .go as the suffix), and start running the generated executable file after linking. Take a look at the actual Operation:

1.$go run helloworld.go


Print: hello,world

The above compilation, linking, and running are all one-time tasks , which means that the next time you run the go run command, all internal processes will be redone. We can generate a binary program through the go build command, and then call it arbitrarily, as shown below:

  1. $go build helloworld.go

  2. $./helloworld

  3. ##hello,world
  4. Here we mentioned compiled language, what is compiled language? If compiled language The written program needs to be understood by the machine. It needs to go through two steps: compilation and linking. Compilation is to compile the source code into machine code, and linking is to concatenate the machine code of each module and the dependent library to generate an executable file.

Let’s take a look at the advantages and disadvantages of compiled languages. Due to the existence of the pre-compilation process, the code can be optimized and only needs to be compiled once. The runtime efficiency will be higher and it can be independent of the language environment. Run, the disadvantage is that the entire modified module needs to be compiled.

Compared with compiled languages, interpreted languages ​​will only translate line by line when running the program. So what is linking? To be precise, linking and loading, that is, performing these two steps after compilation, so that the program can run in memory. Linking is completed through a connector, which links multiple target files into a complete, loadable, and executable target file. The entire process includes symbol resolution (associating the application symbols in the target file with the corresponding definition). two steps) and associating symbol definitions with memory locations.

Naming convention

The names of functions, constants, variables, types, statements, labels, and packages in the GO language have relatively uniform naming rules. The names start with a letter or an underscore, followed by It can be any number of characters, numbers or underscores. Note that the GO language is case-sensitive and keywords cannot be used as names. When encountering names consisting of words, GO programmers generally use the "camel case" style.

Speaking of this, let’s take a look at Java’s naming convention. Taking $ as an example, Oracle’s official website recommends not to use $ or _ to start your variable names, and it is recommended not to use the “$” character at all in naming. The original text is “The convention, however, is to always begin your variable names with a letter, not '$' or '_'". Regarding this article, Tencent has the same view. Baidu believes that although class names can support the use of the "$" symbol, it is only used in system generation (such as anonymous classes, proxy classes), and encoding cannot be used.

This type of problem has been raised by many people on StackOverFlow. The mainstream opinion is that you don’t need to pay too much attention. You only need to pay attention to whether the original code exists "_". If it exists, keep it. If it does not exist, try to keep it. Avoid use. Someone also suggested that the reason why "_" should not be used as much as possible is that low-resolution displays make it difficult for the naked eye to distinguish between "_" (one underline) and "__" (two underlines).

I personally think it may be due to the coding standards of C language. Because in C language, the macro names, variable names, and internal function names in the system header files begin with _, so when you #include the system header files, the names in these files are defined. If they match the names you use Conflicts may cause various strange phenomena. Based on various information, it is recommended not to use "_", "$", or spaces as the beginning of the name, so as not to be difficult to read or cause strange problems.

Regarding class names, the suggestion given by Russian Java expert Yegor Bugayenko is to try to use the abstraction of real-life entities. If the name of the class ends with "-er", this is not a recommended naming method. He pointed out that there is an exception to this article, and that is tool classes, such as StringUtils, FileUtils, and IOUtils. For interface names, don't use IRecord, IfaceEmployee, RedcordInterface, instead use real world entity naming.

Of course, the above are all for Java and have nothing to do with GO. The GO language is more influenced by the C language.

Variable overview

GO language includes four main declaration methods: variable (var), constant (const), type (type) and function (func). Let’s talk about some feelings related to variables:

1. The var declaration creates a variable of a specific type, then attaches a name to it, and sets its initial value. Each declaration has a common form. :var name type = expression. One more thing, the Go language allows empty strings and will not report a null pointer error.

2. You can use name:=expression to declare variables. Note: = means declaration and = means assignment.

If the life of a variable is var x int, the expression &x (address of x) obtains a pointer to an integer variable, and its type is an integer pointer (*int). If the value is called p, we can say that p points to x, or that p contains the address of x. The variable pointed to by p is written *p. The expression *p gets the value of a variable (in this case, an integer). Because *p represents a scalar, it can also appear on the left side of the assignment operator and is used to update the value of the variable.

  1. x:=1

  2. p:=&x//p is an integer pointer, pointing to x

  3. fmt.Println(*p)//Output "1"

  4. ##*p=2//Equivalent to x=2

  5. fmt.Println(x)//Output "2"

Note that compared to Java's NULL, GO means that the zero value of the pointer type is nil.

3. Use the built-in new function to create a variable. The expression new(T) creates an unnamed T type variable, initializes it to a zero value of type T, and returns its address (address type is *T) . There is no difference between variables created using new and ordinary local variables whose addresses are taken, except that there is no need to introduce (or declare) a virtual name and can be used directly in expressions through new(T).

  1. func newInt() *int{

  2. return new(int)

  3. }


Equivalent to:

  1. func newInt() *int{

  2. var dummy int

  3. return &dummy

  4. }

gofmt tool

GO language Many tools are provided, such as gofmt, which can format the code. Let's see how it is implemented.

Gofmt will read the program and format it, such as the gofmt filename command, which will print the formatted code. Let’s take a look at a sample program (program name demo.go):

Hello, I am your little cute GO Language

After running gofmt demo.go, the output code is as follows:

Hello, I am your little cute GO Language

Garbage Collection

For high-level language garbage collectors, how to know whether a variable should be recycled? The basic idea is each package-level variable, and each local variable of the currently executing function , can be used as the source of tracing the path of variables, which can be found through pointers and other means of reference. If the variable's path does not exist, then the scalar becomes inaccessible, so it does not affect any other calculations.

Because the lifetime of a variable is determined by whether it is reachable, a local variable can continue to exist beyond one iteration of the loop that contains it.

The design goal of the garbage collector in the GO language is a non-blocking collector. GO1.5 achieves recycling within 10 milliseconds (note that according to experiments, this statement can only be made when the GC has enough CPU time. can only be established under certain circumstances). From a design principle point of view, Go's recycler is a concurrent, three-color, mark-and-clear recycler. Its design idea was proposed by Dijkstra in 1978. The goal is to be consistent with the properties of modern hardware and modern software. Low latency needs are a perfect match.

Summary

To sum up, every new language appears for a reason, generally speaking there are two main reasons:

1. Complex scenarios or specific problems have emerged that cannot be solved by current mainstream languages;

2. A more cost-effective language is needed.

I think, except for Bell Labs, which will do something completely out of personal feelings, no company will casually lay out new technologies with no way out. As Rob Pike said, "Complexity grows in a multiplicative manner." In order to solve a certain problem, making a certain part of the system more complex little by little inevitably adds complexity to other parts.

Under the constant pressure to add system features, options and configurations, and to release quickly, simplicity is often overlooked. Achieving simplicity requires condensing the essence of the idea at the beginning of the project and developing more specific guidelines throughout the life of the project to discern which changes are good and which are bad or fatal.

With enough effort, good changes can achieve their goals without compromising what Fred Brooks called the "conceptual integrity" of software design. Bad changes don't do this, and fatal changes sacrifice simplicity for convenience. But only through simplicity in design can a system remain stable, secure, and self-consistent as it grows. The Go language not only includes the language itself and its tools and standard libraries, but also maintains a behavioral culture of extreme simplicity.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在 Windows 11 上彻底删除不需要的显示语言的方法在 Windows 11 上彻底删除不需要的显示语言的方法Sep 24, 2023 pm 04:25 PM

在同一设置上工作太久或与他人共享PC。您可能会安装一些语言包,这通常会产生冲突。因此,是时候删除Windows11中不需要的显示语言了。说到冲突,当有多个语言包时,无意中按Ctrl+Shift会更改键盘布局。如果不注意,这将是手头任务的障碍。所以,让我们直接进入方法!如何从Windows11中删除显示语言?1.从设置按+打开“设置”应用,从导航窗格中转到“时间和语言”,然后单击“语言和地区”。WindowsI单击要删除的显示语言旁边的省略号,然后从弹出菜单中选择“删除”。在出现的确认提示中单击“

给语言大模型加上综合视听能力,达摩院开源Video-LLaMA给语言大模型加上综合视听能力,达摩院开源Video-LLaMAJun 09, 2023 pm 09:28 PM

视频在当今社交媒体和互联网文化中扮演着愈发重要的角色,抖音,快手,B站等已经成为数以亿计用户的热门平台。用户围绕视频分享自己的生活点滴、创意作品、有趣瞬间等内容,与他人互动和交流。近期,大语言模型展现出了令人瞩目的能力。我们能否给大模型装上“眼睛”和“耳朵”,让它能够理解视频,陪着用户互动呢?从这个问题出发,达摩院的研究人员提出了Video-LLaMA,一个具有综合视听能力大模型。Video-LLaMA能够感知和理解视频中的视频和音频信号,并能理解用户输入的指令,完成一系列基于音视频的复杂任务,

光动嘴就能玩原神!用AI切换角色,还能攻击敌人,网友:“绫华,使用神里流·霜灭”光动嘴就能玩原神!用AI切换角色,还能攻击敌人,网友:“绫华,使用神里流·霜灭”May 13, 2023 pm 07:52 PM

说到这两年风靡全球的国产游戏,原神肯定是当仁不让。根据5月公布的本年度Q1季度手游收入调查报告,在抽卡手游里《原神》以5.67亿美金的绝对优势稳稳拿下第一,这也宣告《原神》在上线短短18个月之后单在手机平台总收入就突破30亿美金(大约RM130亿)。如今,开放须弥前最后的2.8海岛版本姗姗来迟,在漫长的长草期后终于又有新的剧情和区域可以肝了。不过不知道有多少“肝帝”,现在海岛已经满探索,又开始长草了。宝箱总共182个+1个摩拉箱(不计入)长草期根本没在怕的,原神区从来不缺整活儿。这不,在长草期间

GPT4ALL:终极开源大语言模型解决方案GPT4ALL:终极开源大语言模型解决方案May 17, 2023 am 11:02 AM

开源语言模型生态系统正在兴起,这些生态系统为个人提供综合资源以创建用于研究和商业目的的语言应用程序。本文深入研究GPT4ALL,它通过提供全面的搭建模块,使任何人都能开发类似ChatGPT的聊天机器人,从而超越了特定的使用案例。什么是GPT4ALL项目?GPT4ALL可以在使用最先进的开源大型语言模型时提供所需一切的支持。它可以访问开源模型和数据集,使用提供的代码训练和运行它们,使用Web界面或桌面应用程序与它们交互,连接到Langchain后端进行分布式计算,并使用PythonAPI进行轻松集

吵翻天!ChatGPT到底懂不懂语言?PNAS:先研究什么是「理解」吧吵翻天!ChatGPT到底懂不懂语言?PNAS:先研究什么是「理解」吧Apr 07, 2023 pm 06:21 PM

机器会不会思考这个问题就像问潜水艇会不会游泳一样。——Dijkstra早在ChatGPT发布之前,业界就已经嗅到了大模型带来的变革。去年10月14日,圣塔菲研究所(Santa Fe Institute)的教授Melanie Mitchell和David C. Krakauer在arXiv发布了一篇综述,全面调研了所有关于「大规模预训练语言模型是否可以理解语言」的相关争论,文中描述了「正方」和「反方」的论点,以及根据这些论点衍生的更广泛的智力科学的关键问题。论文链接:https://arxiv.o

学Python,还不知道main函数吗学Python,还不知道main函数吗Apr 12, 2023 pm 02:58 PM

Python 中的 main 函数充当程序的执行点,在 Python 编程中定义 main 函数是启动程序执行的必要条件,不过它仅在程序直接运行时才执行,而在作为模块导入时不会执行。要了解有关 Python main 函数的更多信息,我们将从如下几点逐步学习:什么是 Python 函数Python 中 main 函数的功能是什么一个基本的 Python main() 是怎样的Python 执行模式Let’s get started什么是 Python 函数相信很多小伙伴对函数都不陌生了,函数是可

计算机硬件能直接识别并执行的语言是什么计算机硬件能直接识别并执行的语言是什么Dec 25, 2020 pm 03:16 PM

计算机硬件能直接识别并执行的语言是机器语言。机器语言是机器能直接识别的程序语言或指令代码,无需经过翻译,每一操作码在计算机内部都有相应的电路来完成它。

Azure AI的文本转语音功能已经支持41种多语言语音Azure AI的文本转语音功能已经支持41种多语言语音Aug 10, 2023 pm 07:05 PM

Microsoft的AzureAI文本转语音服务允许你将文本转换为不同语言的语音。今年年初,AzureAI文本转语音引入了JennyMultilingual语音,允许客户跨区域设置以一致的角色生成语音。到目前为止,Jenny多语言语音支持14种语言。今天,Microsoft宣布将多语言语音功能扩展到41种语言和口音。今天,Microsoft还宣布了一个新的男声(RyanMultilingual),作为其多语言产品组合的一部分。这些新语音具有输入文本的自动语言预测功能。因此,这消除了手动标记的需要

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)