Go函数参数中下划线的含义

时间:2016-05-06 19:12:06

标签: go

来自以下函数here。我注意到最后一个参数是用import csv print "Database" print "1)Register people" print "2)View People registered" print "3)Staff Details" choice=input("Enter your choice over here: ") #This part takes the input from the user and then stores it in 'store.txt' if choice==1: a=input("Enter how many people to register: ") for i in range(0,a) : a=(raw_input("Enter the name: ")).capitalize() b=input("Enter contact number: ") c=raw_input("Enter the email ID: ") d=raw_input("Enter your address: ") with open ('store.txt','a') as store: storeWriter=csv.writer(store) storeWriter.writerow([a,b,c]) store.close() #This prints all the data in the file 'store.txt if choice==2: with open('store.txt', 'r') as store: reader = csv.reader(store) print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - " for row in reader: print 'Name:- '+row[0] print 'Phone Number:- '+row[1] print 'Email ID:- '+row[2] print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - " if choice==3: #a=(raw_input("Enter the staff's name: ")).capitalize() a=(raw_input("Enter the staff's name: ")).capitalize() with open('store.txt', 'r') as store: reader = csv.reader(store) for row in reader: if row[0]==a: print row continue else: print "No staff with name %s is found in the database,please try again."%a break if choice==4: print "List" ''' What this program does basically is that it checks the entered name(a).But then if i type the name 'Leroy' it not only prints the first line but also prints "No staff with name Leroy is found in the database,please try again." which is not required.And now if i eneter a as Krut, it prints "No staff with name %s is found in the database,please try again." even though there is a name Krut in the field.How do i get this done (i.e Enter Krut and then it should print row of 'Krut' that is "Krut,5537590,froost1999@outlook.com" Also next i want is that (in choice 4) if they eneter the name , it converts the file's(store.txt) data into a listSo basically what i can do with that would be like print elemenet [0] for name, element[1] for contact number,element[2] for email ID etc.Like it should make the first line of code that is "Leroy,55349234,ok@gmail.com" into a list form ['Leroy','55349234','ok@gmail.com'] so that when the user asks for the contact detail of the specific person i can just print list.(1) to get their phone number. ''' 标识的。这种模式的目的是什么?

_

4 个答案:

答案 0 :(得分:11)

这意味着“忽略该参数”,它们仍然需要最后一个参数的原因是因为他们希望将其作为type Handle传递给具有签名的函数GET

type Handle func(http.ResponseWriter, *http.Request, Params)

如果您只是传递func Index(w http.ResponseWriter, r *http.Request)之类的内容,则不会将其视为type Handle

答案 1 :(得分:5)

_是blank identifier。签名中显示该值未被使用,因此签名仍将与界面的方法匹配。

答案 2 :(得分:1)

正如其他人指出的那样,它是blank identifier。例如,请考虑以下示例:

func main() {
     nums := []int{5, 3, 4}
     max := nums[0]
     for _, num := range nums {
         if num > max {
            max = num
        }
    }
    fmt.Println("max:", max)
}

如果您不打算使用索引值,则可以忽略使用_而不是变量名来存储它。=。

答案 3 :(得分:0)

使用“_”代替参数名称可以履行更高级别“作为参数的功能”的义务,而不会收到有关未使用参数的警告。在您的情况下,我相信编译器被告知忽略所有传入的“POST”数据,因此实际上减少了对“GET”功能的请求。