3

Golang基础入门01 | 简介

 3 years ago
source link: https://studygolang.com/articles/32541
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

介绍

Go编程语言(也称为Golang)是Google开发的一种开放源代码编程语言,用于创建简单,可靠和高效的软件。 Golang值得学习的原因有很多:

  • 由专家开发
  • 语法简单易懂
  • 用于Web开发,尤其是后端开发

在电脑或者您的开发环境中安装Go

Go的安装指南可在此处获得: https://golang.org/dl/

安装完成后,您可以在喜欢的文本编辑器(例如VSCode)中编写Go程序,也可以使用专用的IDE(例如Goland)。

Golang游乐场也可用于学习Go编程语言: https://play.golang.org/

第一个程序

要检查Go是否正确安装在本地计算机中,请输入以下命令。

go version

要检查Go环境变量设置,请使用此命令。

go env

好了,让我们创建一个名为main.go的文件来编写第一个程序。 根据Go环境设置在Go工作空间目录中创建该文件。

让我们在main.go中编写第一个程序:

package main // 声明包装主
import (
    "fmt" // 导入所需的库(例如语法导入“ fmt”包)
)

func main() { // 声明程序的主函数
    // 从fmt包中,使用名为Println()的函数将输出写入控制台
    fmt.Println("Hello, playground")  // 开头字母大写表示该函数在包范围内
}

要执行该程序,请在用于编写该程序的工作目录中运行此命令。

go run main.go

这是输出:

Hello, playground

简而言之,执行流程使用自上而下的方法。

变量

变量是一种将值存储到特定位置的机制。 使用变量,我们可以存储具有特定类型的任何值,例如字符串,整数甚至数组。

Go具有许多类型的值/数据,可以存储在变量中。 Go数据/值类型的列表为:

布尔值:bool (true / false)

数值类型:int,float64,uint8( 其他数值类型

字符串:string

Array:例如 [4]int {1,2,3,4}

Slice:例如 []int {1,2,3,4}

可以在Go语言规范中检查其他类型: https://golang.org/ref/spec#Types

在数字类型中,有两种主要类型,分别称为无符号(uint8,uint32)和有符号(int,float64)。 unsigned的含义仅是类型中可用的正数。 有符号的int和float64可用于正数和负数。

可以在下面的代码中看到变量声明的示例:

package main

import (
    "fmt"
)

var (
    a = 1
    b = 2
    c = 3
) //declare multiple variables

func main() {
    data := "Box this lap" //declare variable with shorthand syntax
    fmt.Println(a, b, c) //print the value of a,b,c
    fmt.Printf("%T %T %T\n", a, b, c) //print the type of a,b,c variable with %T notation
    fmt.Println(data) //print the value of data
    fmt.Printf("%s %T\n", data, data) //print the value with %s notation and the type with %T notation
}

算术运算有数字类型可用,操作数(算术运算中涉及的变量)必须具有相同的类型。 例如,由于类型不同,无法执行uint类型与int类型之间的操作。

可以在下面的代码中看到算术运算的示例:

package main

import (
    "fmt"
)

var a int = 10
var b int = 2

func main() {
    d := a + b 
    e := a - b 
    f := a / b 
    g := a * b
    h := a % b //gives the result of remainder between 10 and 2

    fmt.Printf("The result of %d + %d equals %d\n",a,b,d)
    fmt.Printf("The result of %d - %d equals %d\n",a,b,e)
    fmt.Printf("The result of %d / %d equals %d\n",a,b,f)
    fmt.Printf("The result of %d * %d equals %d\n",a,b,g)
    fmt.Printf("The result of %d modulo %d equals %d\n",a,b,h)
}

类型转换

在Go中,某些类型的值可以转换为另一种类型。 要转换为指定的类型,请使用目标类型,后跟"()"(要从字符串转换为特定类型,请使用strconv.parseType()(示例:strconv.parseInt(" 42",10,64)))

// int -> float64
a := 42
data := float64(a)

// string -> int
a := "43"
data, _ := strconv.Atoi(a)

// int -> string
a := 43
data := strconv.Itoa(a)

要了解有关strconv软件包的更多信息,请查看以下链接: https://golang.org/pkg/strconv/

资料来源

以下是一些有用的资源,可进一步了解Go编程语言

https://golang.org/doc/effective_go.html(编写Go代码的最佳做法)

https://golang.org/ref/spec(Go语言规范)

https://godoc.org/(有关Go软件包和第三方软件包的文档)

我希望本文对帮助学习Go编程语言有所帮助。 如果您有任何想法或反馈,可以在下面的评论留言。

关注公众号【技术全沾】学习更多有趣的编程知识。

有疑问加站长微信联系(非本文作者)

eUjI7rn.png!mobile

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK