Go:基本用于循环和strconv

时间:2016-01-31 03:27:52

标签: go

我是go语言的新生,我想问一些基本的东西,我们怎么能理解这个功能。
我们需要使用“strconv”来解决这个问题。

  package main

import (
    "fat"
    "strconv"

)
type Student struct {
    Name string
}
func (stu *Student) Leave() {
    fmt.Println(stu.Name + " Leaving")

}

func (stu *Student) Present() {
     fmt.Println("I am " + stu.Name)

}

func main() {
  fmt.Println("Start of session")
  for i := 0; i < 6; i++ {
    s := Student{Name: fmt.Sprintf("Student%d", i)}
    s.Present()
    fmt.Println("Room Empty")
    defer s.Leave()
}
  fmt.Println("End of session")
} 

输出应该是这样的

Start of session 
I am Student0
I am Student1
I am Student2
I am Student3
I am Student4
I am Student5 
End of session 
Student5 Leaving 
Student4 Leaving 
Student3 Leaving 
Student2 Leaving 
Student1 Leaving 
Student0 Leaving 
Room Empty

我们只需要编写一个main函数()和一个简单的for循环来获得结果。

1 个答案:

答案 0 :(得分:0)

这是一种方法:

function searchByChannel() {
    var results = YouTube.Channels.list('id,snippet', {
        id: 'UC-9-kyTW8ZkZNDHQJ6FgpwQ',
        maxResults: 25
    });

    var title = "";
    var id = "";
    var lr = 0;
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("Sheet1");
    for (var i = 0; i < results.items.length; i++) {
        var item = results.items[i];
        title = item.snippet.title;
        id = item.id.videoId;
        lr = sheet1.getLastRow() + 1;
        sheet1.getRange(lr, 1).setValue(title);
        sheet1.getRange(lr, 2).setValue(id);
        lr++;
    }
}

playground example

延迟函数以相反顺序从函数返回时执行。