当前位置:网站首页>The Go Programming Language (Introduction)
The Go Programming Language (Introduction)
2022-08-04 22:49:00 【51CTO】
介绍
Gois a procedural programming language.它由 Google 的 Robert Griesemer、Rob Pike 和 Ken Thompson 于2007年开发,但于2009 年Launched as an open source programming language.Programs are assembled by using packages,to efficiently manage dependencies.The language also supports environments that employ patterns similar to dynamic languages.例如,类型推断(y := 0 is a variable of floating point type y a valid statement).
从Go 编程开始
There are various online IDE,例如 The Go Playground、repl.it 等,无需安装即可运行 Go 程序.
为了在自己的 PC 或笔记本电脑上安装 Go,We need the following two softwares:Text editors and compilers
文本编辑器: A text editor gives you a place to write source code平台. Below is a list of text editors:
- Windows 记事本
- Operating system editing commands
- Brief
- Epsilon
- vm 或 vi
- Emacs
- VS 代码
寻找 Go 编译器: Go Distributions are provided in binary form,可用于FreeBSD(版本 8 及更高版本)、Linux、Mac OS X(Snow Leopard 及更高版本)以及具有32 位 (386) 和64 位 (amd64 ) 的Windows 操作系统 ) x86处理器架构.
用 Go 编写第一个程序:
输出:
GoSyntax explanation of the program:
- 第1行:包含程序的main包,Contains the overall content of the program,是程序运行的起点,必须写.
- 第 2 行:它包含import “fmt”,It is a preprocessor command,Tell the compiler to include files located in packages.
- 第 3 行:main 函数,is the start of program execution.
- 第 4 行:fmt.Println()是一个标准库函数,Used to print something on the screen as output.在此,fmtThe package is passed for displaying output Println 方法.
- 注释:注释用于解释代码,其使用方式与 Java、C 或 C++ 中的类似.编译器忽略注释条目并且不执行它们.注释可以是单行或多行.
单行注释:
语法:
多行注释:
语法:
下面是另一个例子:
输出:
上述程序说明:
在上述程序中,Same package line、Same import line、The same function declaration and usage as we are in第一个 GO 程序中使用的相同的 Println 函数.这次不是打印字符串 “Hello, go”, 而是打印字符串 1 + 1 = 后跟表达式 1 + 1 的结果.这个表达式由三部分组成:数字文字 1(它的类型为 int )、+ 运算符(表示加法)和另一个数字文字 1.
为什么“Go语言”是这样的?
因为 Go The language strives to combine the programming ease of interpreted dynamically typed languages with the efficiency and safety of statically typed compiled languages.It also aims to modernize,Supports network and multi-core computing.
Go What other languages are excluded from?
- Go Trying to reduce the amount of typing for both meanings of a word.在整个设计过程中,Developers try to reduce confusion and complexity.
- There are no forward declarations,也没有头文件;Everything is declared only once.
- 使用:=declare-and-initialize Constructed simple type derivation can reduce stuttering.
- There is no type hierarchy:类型只是,They don't have to declare their relationship.
硬件限制
我们观察到,十年来,Hardware and processing configurations change very slowly.2004 年,P4 的时钟速度为 3.0 GHz,而现在在 2018 年,Macbook pro 的时钟速度为 Approx (2.3Ghz v 2.66Ghz).为了加快速度,We use the power of more processors,But the cost of using more processors also increases.由于这个原因,We use limited processors,And use a limited processor,We have a heavy programming language,Its threads take up more memory and reduce the performance of our system.因此,为了解决这个问题,Golang The way it is designed is to use Goroutine 代替线程,It is similar to threads,But it consumes very little memory.
Like thread consumption 1MB 而 Goroutine 消耗 2KB 内存一样,So we can trigger millions of them at the same time goroutine.So the point discussed above makes golang 成为像 C++ 和 Java A powerful language that handles concurrency as well.
Go语言的优缺点
优点:
- 灵活——简洁、Simple and easy to read.
- 并发性-It allows multiple processes to run efficiently at the same time.
- quick results - Its compile time is very fast.
- 库 - It provides a rich standard library.
- 垃圾收集 - 这是 Go 的一个关键特性.Go Good at having a lot of control over memory allocation,And significantly reduced latency in the latest version of the garbage collector.
- It validates interface and type embedding.
缺点:
- 它不支持泛型,即使有很多关于它的讨论.
- Packages distributed in this programming language are very useful,但 Go Not so object-oriented in the traditional sense.
- 缺少一些库,尤其是 UI 工具包.
用 Go Some popular applications developed by the language
- Docker:A set of deploymentslinux容器的工具
- Openshift:Red Hat's cloud computing platform as a service.
- Kubernetes:无缝自动化部署流程的未来
- Dropbox:Move some of their key components from Python 迁移到 Go.
- Netflix:Two parts of their server architecture.
- InfluxDB:是由 InfluxData 开发的开源时间序列数据库.
- Golang:Language itself is used Go 编写的.
Go语言的特点
- 语言设计:语言of designers consciously keep the language simple and easy to understand.The entire details are within a few pages,Some interesting design decisions are made through the language的面向对象support made.为此,The language is opinionated,and recommended one惯用的实现方式.It prefers composition over inheritance.在 Go 语言中,“事半功倍”是口号.
- 包管理:Go Incorporates modern developer processing开源项目的工作流程,And include it in the way you manage external packages.Provide support directly in the tool to fetch external packages and publish your own with a simple set of commands.
- 强大的标准库:Go 有强大的标准库,It is distributed as a package.
- 静态类型:Go 是静态类型语言.因此,在这个编译器中,Not only can the code compile successfully,Also ensures type conversion and compatibility.由于这个特性,Go Avoids all the problems we face in dynamically typed languages.
- 测试支持:Go It itself provides us with unit testing capabilities,i.e. a simple mechanism to write unit tests in parallel with your code,So you can understand code coverage through your own tests.作为示例,This can easily be used to generate code documentation.
- 平台无关:Go 语言就像 Java 语言一样,Support platform is independent.Due to its modular design and modularity,That is, the code is compiled and converted into the smallest possible binary form,因此不需要依赖.Its code can be compiled on any platform or any server and application you work on.
边栏推荐
猜你喜欢
随机推荐
Deep Learning RNN Architecture Analysis
【游戏建模模型制作全流程】使用ZBrush制作骷髅王
剑指Offer | 数值的整数次方
rk3399-0.0 svc command
「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
JVM memory configuration parameter GC log
2022精选最新金融银行面试真题——附带答案
【内存操作函数内功修炼】memcpy + memmove + memcmp + memset(四)
MQTT[一]基础知识介绍
ffplay视频播放原理分析
Leaflets of three bouquet of roses
软测人面试 ,HR 会问到哪些问题?学会涨薪3000+
Latex fast insert author ORCID
逆序对的数量
今天又做了三个梦,其中一个梦梦里的我还有意识会思考?
[Mock Interview - 10 Years of Work] Are more projects an advantage?
typeScript-部分应用函数
go语言的日志实现(打印日志、日志写入文件、日志切割)
go语言的time包介绍
【3D建模制作技巧分享】ZBrush模型如何添加不同材质