Beispiel #1
0
func postFile(filename string,targetUrl string) error {
	bodyBuf :=&bytes.Buffer()
	bodyWriter :=multipart.NewWriter(bodyBuf)
	fileWriter,err := bodyWriter.CreateFormFile("uploadfile",filename)
	if err != nil {
		fmt.Println("error writing to buffer")
		return err
	}
	fh,err := os.Open(filename)
	if err != nil {
		fmt.Println("error opening file")
		return err
	}
	_,err = io.Copy(fileWriter,fh)
	if err != nil {
		return err
	}
	contentType := bodyWriter.FormDataContentType()
	bodyWriter.Close()
	resp,err := http.Post(targetUrl,contentType,bodyBuf)
	if err != nil {
		return err
	}
	def resp.Body.Close()
	resp_body , err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	fmt.Println(resp.Status)
	fmt.Println(string(resp_body))
	return nil
}
Beispiel #2
0
func ExampleValidateFunc() {
	ui := &UI{
		// In real world, Reader is os.Stdin and input comes
		// from user actual input
		Reader: bytes.Buffer("Y\n"),
		Writer: ioutil.Discard,
	}

	query := "Do you love golang [Y/n]"
	ans, _ := Ask(query, &Options{
		// Define validateFunc to validate user input is
		// 'Y' or 'n'. If not returns error.
		ValidateFunc: func(s string) error {
			if s != "Y" && s != "n" {
				return fmt.Errorf("input must be Y or n")
			}

			return nil
		},
	})

	fmt.Println(ans)
	// Output: Y
}