当前位置:网站首页>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
边栏推荐
- 少儿编程结构的改变之路
- redis优化系列(二)Redis主从原理、主从常用配置
- FileProvider 路径配置策略的理解
- Understanding of MQ
- Cognition and R & D technology of micro robot
- Redis optimization series (II) redis master-slave principle and master-slave common configuration
- MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
- 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
- stylecloud ,wordcloud 库学习及使用例子
- Implementation of inserting millions of data into MySQL database in 10 seconds
猜你喜欢
随机推荐
nacos基础(6):nacos配置管理模型
Learn go language 0x06: Fibonacci closure exercise code in go language journey
博客文章导航(实时更新)
积极参与中学机器人竞赛的意义
Learn go language 0x05: the exercise code of map in go language journey
Laravel always returns JSON response
分享两个实用的shell脚本
mysql中整数数据类型tinyint详解
R-Drop:更强大的Dropout正则方法
Summary of the relationship among GPU, CUDA and cudnn
Compress the curl library into a sending string of utf8 and send it with curl library
Blog post navigation (real-time update)
Write console script by laravel
My creation anniversary
Implementation of inserting millions of data into MySQL database in 10 seconds
怎么进行固定资产盘点,资产盘点报告如何一键生成
Prevent SQL injection in web projects
卷积层和池化层总结
Maker education for primary and middle school students to learn in happiness
探究机器人教育的器材与教学