Jorgen's blog Jorgen's blog
首页
  • 平台架构
  • 混合式开发记录
  • 推送服务
  • 数据分析
  • 实时调度
  • 架构思想

    • 分布式
  • 编程框架工具

    • 编程语言
    • 框架
    • 开发工具
  • 数据存储与处理

    • 数据库
    • 大数据
  • 消息、缓存与搜索

    • 消息队列
    • 搜索与日志分析
  • 前端与跨端开发

    • 前端技术
    • Android
  • 系统与运维

    • 操作系统
    • 容器化与 DevOps
  • 物联网与安全

    • 通信协议
    • 安全
    • 云平台
收藏
  • 关于我
  • 终身学习
  • 关于时间的感悟
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

jorgen

Love it, make mistakes, learn, keep grinding.
首页
  • 平台架构
  • 混合式开发记录
  • 推送服务
  • 数据分析
  • 实时调度
  • 架构思想

    • 分布式
  • 编程框架工具

    • 编程语言
    • 框架
    • 开发工具
  • 数据存储与处理

    • 数据库
    • 大数据
  • 消息、缓存与搜索

    • 消息队列
    • 搜索与日志分析
  • 前端与跨端开发

    • 前端技术
    • Android
  • 系统与运维

    • 操作系统
    • 容器化与 DevOps
  • 物联网与安全

    • 通信协议
    • 安全
    • 云平台
收藏
  • 关于我
  • 终身学习
  • 关于时间的感悟
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Go学习指南
  • Golang入门
    • Go youtube booking-app
      • Basic Info
    • first code
      • hello world
    • Variable
      • printf
    • Data Types
    • Get user input
    • Array
    • Slice
    • Loops
    • Condition
    • Functions
    • goroutine
    • Waitgroup
  • DS&A
  • 算法碎碎念
  • programming_languages
Jorgen
2023-12-31
目录

Golang入门

# Go youtube booking-app

# Basic Info

  1. 2007 google 2009 open source

go

  1. use cases

    1. for performant applications
    2. runing on scaled distributed systems
  2. characterics

    1. simple syntax, Easy to learn read and write code
    2. fast build time start up and run
    3. require fewer resources
    4. faster than interpreted languages like python
    5. consistent across different os
  3. install

  4. vscode:go extension Go team at Google

# first code

# hello world

  1. init module go mod init <module path>
  2. all our code must belong to **packages — the first statement in Go file must be ‘package…’**
  3. the “main function” is the entrypoint of a Go program
  4. A program can only have 1 main function because you can only have 1 entrypoint
  5. Go packages:
    1. Go programs are organized into packages
    2. Go’s standard library, provides different core packages for us to use
    3. ‘fmt’ is one of these, which you can use by importing it
    4. a package is a collection of source files
  6. go run <file name> = compiles and runs the code

# Variable

  1. Variable must be used
  2. Constants are like variables, except their values cannot be changed.

# printf

pkg.go.dev/fmt:print formatted data

  • It takes a template string that contains the text that need be formatted.
  • plus some annotation verbs(placeholder) that tells the fmt functions how to format the variable passed in.

# Data Types

Go is a statically typed language

  • you need to tell go compiler, the data type when declaring the variable
  • Type Inference: BUT Go can infer the type when you assign a value
Go Java
int8 byte
int16 short
int32 int
int64 long

sytactic suger in Programming

  • a term to describe a feature in a language that let you do smth more easily
  • make the language ‘sweeter’ to human use
  • But doesn’t add any new functionality that it didn’t already have

# Get user input

fmt → formatted Input and Output(I/O)

  • Print Messages
  • Collect User Input
  • Write into a File

Pointer

  • A pointer is a variable that points to the memory address of another variable
  • a special variable

# Array

  • Fixed Size how many elements the array can hold
  • Only the same data type can be stored.
var variable_name [size]data_type
1

# Slice

  • Slice is an abstraction of the array
  • Slices are more flexible and powerful: variable-length or get an sub-array of its own
  • Slices are also index-based and have a size, but is resized when needed

append

  • Adds the element at the end of the slice
  • Grows the slice if a greater capacity is needed and return the updated slice value

# Loops

  • In general, Languages provide various control structures to control the application flow
  • a loop statement allows us to execute code multiple times, in a loop
  • only have ‘for’ loop

Range

  • Range iterates over elements for different data structures (so not only arrays and slices)
  • For arrays ans slices, range provides the index and value for each element.

string.Fields()

  • Splits the string with white space as separator
  • And return a slice with the split elements

Blank identifier

  • to ignore a variable you don’t want to use
  • So with Go you need to make unused variables explicit

# Condition

if statement {}

break statement

  • Terminate the for loop
  • And continues with the code right after the for loop

continue statement

  • Cause loop to skip the remainder of its body
  • And immediately retesting its condition

switch statement

  • allows a variable to be tested for equality against a list of values
  • default handle the case, if not match is found

# Functions

  • Encapsulate code into own container which logically belong together

  • Like variable name, you should give a funtion a descriptive name

  • call the function by its name, whenever you want to execute this block of code

  • every program has at least one funtion, which is the main() function

  • Funtion is only executed when called

  • you can call a function as many times you want

  • so funtion is also used to reduce code duplication

parameters

  • information can be passed into funtions as parameters
  • parameters are also call arguments

Mutiple packages

exporting a variable

  • make it avaiable for all packages in the app
  • Capitalize first letter

3 level of scope

local

  • declaration within function → can be used only with that function
  • declaration within block → can be used only within that block

package

  • declaration outside all funtions → can be used everywhere in the same package

global

  • declaration outside all functions & uppercase first letter → can be used everywhere across all packages

variable scope

scope is the region of a program, where a defined variable can be accessed

maps

  • maps unique keys to values
  • you can retrieve the value by using its key later
  • all keys have same data types — all values have same data types

struct

the type keyword creates a new type, with the name you specify

In fact, you can create a type based on every other data type like int, string etc

# goroutine

concurrency

go keyword

  • “go…” starts a new goroutine
  • A goroutine is a lightweight thread managed by Go runtime

# Waitgroup

  • wait for the launched goroutine to finish
  • Package “sync” provides basic synchronization functionality
  • Add: sets the number of goroutines to wait for (increases the counter by the provided number)
  • Wait: Blocks until the WaitGroup counter is 0
  • Done: decrements the WaitGroup counter by 1 so this is called by the goroutine to indicate that its finished

built-in functionality for goroutine to talk with one another

comparison to other languages

| writing concurrent code | writing concurrent code is more complex

more overhead
Threads vs Goroutines
heavyweight and needs more hardware resources

why? what is the different?

#go
上次更新: 2025/03/09, 15:45:50
Go学习指南
DS&A

← Go学习指南 DS&A→

最近更新
01
STM32入门
03-09
02
ADB调试
03-09
03
微信小程序学习记录
02-09
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jorgen | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式