动态设置结构的键和值

时间:2018-12-08 10:53:59

标签: go

我的问题是,我有一个具有非固定值的键,就像在python中一样,我们可以这样做:

dict_ = {}
data_ = [{"name": "roy", "text": "yay i got a gift"},{"name": "dep", "text": "my mum gimme a gift"},{"name": "roy", "text": "another gift from my fan"}]
for data in data_:
    key = data["name"]
    if key in dict_:
        dict_[key] += 1 // add more 1 if key already in dict_
    else:
        dict_[key] = 1 // set dict key with starting value 1

我正在尝试用golang做到这一点,谢谢

1 个答案:

答案 0 :(得分:1)

这是等效的Go代码:

Option Explicit
Public Sub LoopLinks()
    Dim ie As Object, ws As Worksheet, arr(), i As Long
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set ie = CreateObject("InternetExplorer.Application")
    arr = Application.Transpose(ws.Range("A1:A3").Value) '<== amend range as required

    With ie
        .Visible = True

        For i = LBound(arr) To UBound(arr)
            If InStr(arr(i, 1), "http") > 0 Then '<=check is an URL
                .Navigate2 arr(i)

                While .Busy Or .readyState < 4: DoEvents: Wend

                'do something......
            End If
        Next
        .Quit
    End With
End Sub

Playground