多行中的多个返回值

时间:2018-09-20 10:43:48

标签: go return return-type

如何在GoLang中的多行中返回多个值?

List<WebElement> activeLinks = new ArrayList<WebElement>();

        //2.Iterate LinksList: Exclude all the links/images - doesn't have any href attribute and exclude images starting with javascript.
        boolean breakIt = true;
        for(WebElement link:AllTheLinkList) 
        {
            breakIt = true;
            try
            {
                //System.out.println((link.getAttribute("href")));
                if(link.getAttribute("href") != null && !link.getAttribute("href").contains("javascript") && link.getAttribute("href").contains("pharmacy")) //&& !link.getAttribute("href").contains("pharmacy/main#"))
                {
                    activeLinks.add(link);          
                }   
            }
            catch(org.openqa.selenium.StaleElementReferenceException ex)
            {
                breakIt = false;
            }
            if (breakIt) 
            {
                continue;
            }
        }       

        //Get total amount of Other links
        log.info("Other Links ---> " + (AllTheLinkList.size()-activeLinks.size()));
         //Get total amount of links in the page
        log.info("Size of active links and images in pharmacy ---> "+ activeLinks.size());

        for(int j=0; j<activeLinks.size(); j++) {

            HttpURLConnection connection = (HttpURLConnection) new URL(activeLinks.get(j).getAttribute("href")).openConnection();

            connection.setConnectTimeout(4000);
            connection.connect();
            String response = connection.getResponseMessage(); //Ok
            int code = connection.getResponseCode();
            connection.disconnect();
            //System.out.println((j+1) +"/" + activeLinks.size() + " " + activeLinks.get(j).getAttribute("href") + "---> status:" + response + " ----> code:" + code);
            log.info((j+1) +"/" + activeLinks.size() + " " + activeLinks.get(j).getAttribute("href") + "---> status:" + response + " ----> code:" + code);  
        }           
  

./ example.go:9:3:语法错误:意外},需要表达式

1 个答案:

答案 0 :(得分:5)

这不是复合文字或函数调用,您不得在最后一行后面加上逗号:

return req.FormValue("a"),
  req.FormValue("b"),
  req.FormValue("c"),
  req.FormValue("d"),
  req.FormValue("e")

查看示例:

func f() (int, int, string) {
    return 1,
        2,
        "3"
}

测试:

fmt.Println(f())

输出(在Go Playground上尝试):

1 2 3

查看相关问题:How to break a long line of code in Golang?