无法添加Go的sync.Map加载结果

时间:2018-06-15 03:58:59

标签: go

我只是想在sync.Map上做一些简单的CREATE或ADD

gore> :import sync
gore> var sm sync.Map
gore> sm.Store(12345,1)
gore> result, ok := sm.Load(12345)
1
true
gore> newr := result +1
# command-line-arguments
/var/folders/kl/n95_c8j15wn1784jmsq08mq80000gn/T/112740772/gore_session.go:21:17: invalid operation: result + 1 (mismatched types interface {} and int)
error: exit status 2
exit status 2
repl上的{p> result1,但无法添加1

1 个答案:

答案 0 :(得分:1)

  

The Go Programming Language Specification

     

Type assertions

     

对于接口类型的表达式x和类型T,表示主要的   表达

x.(T)
     

断言x不是nil,并且存储在x中的值是T类型。   符号x。(T)称为类型断言。

错误消息告诉所有人:

invalid operation: result + 1 (mismatched types interface {} and int)

result interface {} int类型package main import ( "fmt" "sync" ) func main() { var sm sync.Map sm.Store(12345, 1) result, ok := sm.Load(12345) fmt.Println(result, ok) newr := result.(int) + 1 fmt.Println(newr) } 使用类型断言:

1 true
2

游乐场:https://play.golang.org/p/qotBVR4fSNV

输出:

<mat-form-field>
 <mat-select placeholder="Semester" [(value)]="choosenSub">
  <mat-option *ngFor="let sem of Semester" [value]="sem.subjectDetails">
   {{ sem.queueName }}
  </mat-option>
 </mat-select>
</mat-form-field>
 <br>
<mat-form-field>
 <mat-select placeholder="Subject">
  <mat-option *ngFor="let sub of choosenSub" [value]="sub.subjectName" >
   {{ sub.subjectName }}
  </mat-option>
 </mat-select>
</mat-form-field>