当前位置:网站首页>Typescript and go --- essence
Typescript and go --- essence
2022-06-25 13:52:00 【Favorite truffle chocolate】
A formal parameter is a copy of the contents of an argument , To modify an argument, you must pass in the address
map and chan, Slices are all pointer types , So you can directly assign values to functions , No need to take the address
When the pointer operates , No need to go again *p Value operation , You can directly call methods and variables with pointers , because GO Automatically added
var phone Phone
phone = new(NokiaPhone)
phone.call()s :=make(map[int]*person){
1:&user{"jack“,20}
}
s[1].name = "li"
Use new Function to allocate memory for a new struct variable , It returns a pointer to allocated memory :var t *T = new(T)
var t *T = new(T)
type struct1 struct {
i1 int
f1 float32
str string
}
func main() {
ms := new(struct1)
ms.i1 = 10
ms.f1 = 15.5
ms.str= "Chris"
}map Same as interface, They're all pointer types
// here x You get the address
m := make(map[string]int)
func text(x map[string]int){
fmt.Printf(x)
}
struct Anonymous fields implement inheritance , Will inherit the previous method
type Student struct {
Person // Anonymous fields implement inheritance ( Structure nested structure )
//p Person // It can also be a structure Person names
score int
}
var b *Books
b = &Book1
fmt.Println(b) //&{Go Language www.runoob.com Go Language course 6495407}
fmt.Println(*b) //{Go Language www.runoob.com Go Language course 6495407}
fmt.Println(&b) //0xc000082018
fmt.Println(Book1) //{Go Language www.runoob.com Go Language course 6495407}chan:
The main thread does not wait for the child thread to end , Unless time.Sleep
chonlyread := make(<-chan int) // ( Out ) Create read only channel
chonlywrite := make(chan<- int) //( Enter into ) Create write only channel
chan The difference between buffered and unbuffered : Every few elements can block another GO Threads
Monitor multiple channels , It can be used at this time select select{case n = <-ch:}
close(ch) // End channel
case <-time.After(time.timeOut) // You can exit without waiting for an abnormal error
if x,ok := ch; ok{} for x:= range ch
var wg sync.WaitGroup wg.add(lenth) wg.Done() wg.Wait() // Number of open tasks , End task ,wait Used to block subsequent programs until the end of the channel
interface It's a type of , The default is a pointer .interface Types can define a set of methods , But these don't need to be realized . also interface Can't contain any variables .
The structure implementation interface can be used as a class new
No, let、 The colon 、 Brackets 、 function func、 Define the method for the structure 、 Structure pointer 、 Defining interfaces , The structure implements the interface method , Array of structs ,map Structure
Increase slice capacity make([]int,3,5) append(numbers, 2,3,4) make(chan int,10) make(map[string]int) make([]int{},1,2)
typescript Interfaces can define methods
func getAverage(arr []int, size int) float32 // Distinguish between definition and initialization
defer Only after all statements in the current function have been executed , Is the scope in the current function And exist at the same time defer
Execute in reverse order
panic recover
Interface
x,ok :=m["b"]
for index,value := range arr // It's kind of like foreach How to write it
x,_ := On behalf of ignore
fallthrough Ignore case Conditions continue
TypeScript There is only one type of number in , No, byte、short、int、uint、long、float、double Other types , and TypeScript Of number It's actually a 64 Is a double precision floating-point number , It can be seen as... In other languages double type .
let x: number =10
let arr: number[] = [1,2]
let a: [string,number] = [22,"a"]
let xx: any = 2
var hello : string|number
var foo = (x:number) =>10+x
function name(x: string,b: number = 0.6):number{
retrun parseInt(b)+x
}
name(10,20)
functionbulid(first:strting,....seconde:number[]) {
retrun first + seconde
}
let name = buildType("jsonk",2,3,4,5)
// Think of an interface as a type
interface a{}
interface b{}
interface c extends a,b{}
var name :c ={
v1:12
v2:22
}
interface Person {
name: string;
age?: number; // Add... After the attribute ? Indicates that the attribute is optional
[propName: string]: any; // It means that you can add any number of attributes , The added attribute type is any
}
class Car {
engine:string,
constructor(engine:string){
this.engine = engine
}
disp():void{
}
}
class hello extends Car {
static num:number
static disp():void{
super.disp()
console.log()
}
private str2:string = "world"
}
var obj = new hello()
let status = obj instaceof hello
class hello implements name{
constructor(num:number,helo:string){
this.num = num
this.hello = height
}
}
export interface name {}
import name = require("/name")
export class Circle implements shape.index{
}
import Circle = require ("/Circle")
边栏推荐
- Is it unsafe to make new debts
- leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
- Custom vertical table
- 网络远程访问的方式使用树莓派
- 数据采集系统网关采集工厂效率
- Win7显示屏幕亮度在哪里可以调节
- Gorm-- search you don't know
- Accidentally modify or delete the system variable path to restore
- Explanation of a textbook question
- Cesium learning notes
猜你喜欢

Simple realization of mine sweeping
![[proteus simulation] 51 MCU +ds1302+lcd1602 display](/img/02/9644415b72c330afa15060d81d4cbc.png)
[proteus simulation] 51 MCU +ds1302+lcd1602 display

关于猜数字游戏的实现

Problems encountered in debugging botu TCP connection with debugging tool

电脑必须打开的设置

Drago Education - typescript learning

How to determine if a web worker has been created or closed

As a software testing engineer, how do you think to ensure software quality?

Scope of ES6 variable

Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
随机推荐
##脚本编写ssh免密功能
Rust,程序员创业的最佳选择?
Leetcode: Sword finger offer II 091 Painting house [2D DP]
Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud
turtlebot+lms111+gmapping实践
leetcode:456. 132 mode [monotone stack]
【开源鸿蒙系统展示】RK3568开发板搭载OpenHarmony 3.1 Release
Realization of neural networks with numpy
关于一个图书小系统的实现
Openstack learning notes (I)
How to solve SQL import
如何在 2022 年为 Web 应用程序选择技术堆栈
测一测你的挣钱能力有多强?未来的你将从事什么职业?
Parabolic motion in unity 2D games
启牛是正规的吗?股票开户安全吗?
Getting started with numpy Library
数据库中显示error1822,error1824
Gorm-- search you don't know
深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
Vscode--- format setting configuration