当前位置:网站首页>golang之筆試題&面試題01
golang之筆試題&面試題01
2022-04-23 11:26:00 【runscript.sh】
就是幾個簡單的筆試題目,也可能面試的時候直接給看讓說結果。
1, 用不同的goroutine去操作map的時候會存在線程同步的問題,把map換成int ,這個問題同樣存在。在go裏是這樣。
2,下面這段代碼輸出的值為:
func t(){
jsonStr:=[]byte(`{"age":1}`)
var value map[string]interface{
}
json.Unmarshal(jsonStr,&value)
age:=value["age"]
fmt.Println(reflect.TypeOf(age))
//float64
}
區別:
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,下邊代碼是否有問題,問題在哪裏
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
}
問題在於,ages沒有暴露背外部的包,導致調用者無法初始化ages。
然後在調用add函數時,就會報錯。考察函數的作用域問題。
4,下邊代碼的輸出結果是什麼?
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)
}
輸出結果如下:這個是在考察數組和切片用s2截取s1的下邊為1及之後;而後執行操作。
[12 13]
[22 23 14]
5,下邊代碼輸出什麼
func TestDoit(){
doit:= func(arg int) interface{
}{
var result *struct{
}=nil
if (arg>0) {
result = &struct{
}{
}
}
return result
}
//輸出結果。
//-1:result: <nil> 為空的匿名結構體
//1://result: &{} 匿名結構體的地址
if res:=doit(1);res!=nil{
fmt.Println("result:",res)
}
}
output:
result: &{
}
6,下邊代碼的輸出結果是什麼
//放在main裏邊
//指定只能用一個邏輯處理器,方便看調度順序。
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
這個要注意,第一個for裏沒有傳參數,第二個傳參數了。
所以第一個for裏啟動的goroutine用的i其實是主線程裏的i;
之所以都是10,(也有可能前面幾個<10);是因為調用i的時候,i在主線程內已經加到10了。
而第二個for裏的i是通過參數傳遞,所以會打印0~9;
版权声明
本文为[runscript.sh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231126068630.html
边栏推荐
猜你喜欢

实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题

Usage of rename in cygwin

Understanding of MQ

Interprocess communication -- message queue

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

Interpretation of 2022 robot education industry analysis report

Analyze the rules for the use of robots with good performance

简易投票系统数据库设计

Analyzing the role of social robots in basic science

Significance of actively participating in middle school robot competition
随机推荐
qt5. 8. You want to use SQLite in the 64 bit static library, but the static library has no method to compile the supporting library
得物技术网络优化-CDN资源请求优化实践
C#的学习笔记【八】SQL【一】
解析社交性机器人对基础科学的作用
Redis optimization series (II) redis master-slave principle and master-slave common configuration
怎么进行固定资产盘点,资产盘点报告如何一键生成
redis优化系列(二)Redis主从原理、主从常用配置
Structure of C language (Advanced)
微型机器人的认知和研发技术
rebbitMQ的简单搭建
AcWing 1874. 哞加密(枚举,哈希)
Summary of the relationship among GPU, CUDA and cudnn
PDMS soft lithography process
nacos基础(6):nacos配置管理模型
Advanced file IO of system programming (13) -- IO multiplexing - Select
积极参与中学机器人竞赛的意义
laravel编写Console脚本
Analyzing the role of social robots in basic science
Laravel admin form validation
学习 Go 语言 0x01:从官网开始