当前位置:网站首页>Golang Pen & interview 01
Golang Pen & interview 01
2022-04-23 11:26:00 【Runscript. Sh】
Juste quelques questions simples pour l'examen écrit.,Ou peut - être qu'ils l'ont montré directement à l'entrevue..
1, Avec différentsgoroutineÀ l'opération.mapIl y a un problème de synchronisation des threads,Prends ça.mapRemplacer parint ,C'est le même problème..IngoC'est ça..
2,La valeur de sortie du code suivant est:
func t(){
jsonStr:=[]byte(`{"age":1}`)
var value map[string]interface{
}
json.Unmarshal(jsonStr,&value)
age:=value["age"]
fmt.Println(reflect.TypeOf(age))
//float64
}
La différence:
func t() {
jsonStr := []byte(`{"age":1}`)
var value map[string]int
json.Unmarshal(jsonStr, &value)
age := value["age"]
fmt.Println(reflect.TypeOf(age))
//int
}
3,Y a - t - il un problème avec le Code inférieur?,Quel est le problème?
import (
"sync"
"fmt"
)
type UserAges struct {
ages map[string] int
sync.Mutex
}
func (u *UserAges)Add(name string,age int) {
u.Lock()
defer u.Unlock()
u.ages[name] = age
}
func (u *UserAges)Get(name string)int{
if age,ok:=u.ages[name];ok{
return age
}
return -1
}
Le problème, c'est que,ages Pas de sac exposé à l'extérieur du dos , Fait en sorte que l'appelant ne puisse pas initialiser ages.
Et puis en appelantaddFonction,Il y aura une erreur. Examiner la question de la portée de la fonction .
4, Quel est le résultat de sortie du Code inférieur ?
func TestArrayAndSlice(){
s1:=[]int{
1,2,3}
s2:=s1[1:]
for i:=range s2{
s2[i]+=10
}
fmt.Println(s2)
s2=append(s2, 4)
for i:=range s2{
s2[i]+=10
}
fmt.Println(s2)
}
Les résultats sont les suivants:: C'est pour examiner les tableaux et les tranches s2Interceptions1 En dessous de 1Et après; Puis faites l'opération .
[12 13]
[22 23 14]
5, Qu'est - ce que la sortie de code ci - dessous
func TestDoit(){
doit:= func(arg int) interface{
}{
var result *struct{
}=nil
if (arg>0) {
result = &struct{
}{
}
}
return result
}
//Résultats obtenus.
//-1:result: <nil> Structure anonyme vide
//1://result: &{} L'adresse de la structure anonyme
if res:=doit(1);res!=nil{
fmt.Println("result:",res)
}
}
output:
result: &{
}
6, Quel est le résultat de sortie du Code inférieur
//Mettez - le surmainÀ l'intérieur.
// Spécifie qu'un seul processeur logique peut être utilisé , Pour faciliter la visualisation de l'ordre de répartition .
func t2(){
runtime.GOMAXPROCS(1)
wg:=sync.WaitGroup{
}
wg.Add(20)
//step 1
for i:=0;i<10 ;i++ {
go func() {
fmt.Println("i",i)
wg.Done()
}()
}
fmt.Println("--------------------")
//step 2
for j:=0;j<10 ;j++ {
go func(j int) {
fmt.Println("j",j)
wg.Done()
}(j)
}
wg.Wait()
}
output:
--------------------
j 9
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
j 0
j 1
j 2
j 3
j 4
j 5
j 6
j 7
j 8
Attention à ça,Le premierfor Il n'y a pas de paramètre passé , Le deuxième paramètre passe .
Donc le premierforÇa a commencégoroutine- Oui.i En fait, c'est dans le fil principal i;
La raison pour laquelle 10,( Peut - être quelques - uns avant <10); C'est parce que l'appel iQuand,i A été ajouté au fil principal 10C'est.
Et le secondforÀ l'intérieur.i Est passé par paramètre ,Donc ça imprime0~9;
版权声明
本文为[Runscript. Sh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231126068630.html
边栏推荐
- 积极参与中学机器人竞赛的意义
- R-Drop:更强大的Dropout正则方法
- Learn go language 0x03: understand the dependency between variables and initialization order
- Overall plan management mode in maker Education
- 详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
- 简易投票系统数据库设计
- Laravel always returns JSON response
- MySQL索引优化之分页探索详细介绍
- Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
- redis优化系列(二)Redis主从原理、主从常用配置
猜你喜欢

MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video

Write console script by laravel

Advanced file IO of system programming (13) -- IO multiplexing - Select

R-Drop:更强大的Dropout正则方法

Analyzing the role of social robots in basic science

解读2022机器人教育产业分析报告

Cygwin 中的 rename 用法

Laravel绑定钉钉群警报(php)

系统编程之高级文件IO(十三)——IO多路复用-select

Understanding of MQ
随机推荐
MQ的了解
PDMS soft lithography process
MySQL分区表实现按月份归类
Learn go language 0x06: Fibonacci closure exercise code in go language journey
MQ is easy to use in laravel
配电房远程综合监控系统在10kV预制舱项目中的应用
mysql插入datetime类型字段不加单引号插入不成功
qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
PCB的注意事项
MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
Cognition and R & D technology of micro robot
Analyzing the role of social robots in basic science
Detailed introduction to paging exploration of MySQL index optimization
Laravel adds custom helper functions
年度最尴尬的社死瞬间,是Siri给的
Prevent SQL injection in web projects
Analyze the rules for the use of robots with good performance
Oracle connectivity test gadget
Learn go language 0x07: stringer exercise code in go language journey
Oracle连通性测试小工具