The CTO of my current employer is trying to move everything to Golang; to reduce dev cost (compared to C++), and reduce resource use (compared to more squidgy languages, like PHP). So I now have some experience in this not very web centric programming language. Compared to PHP, the compile error messages are awkward; and somethings takes ages. The HTTP frameworks have many less features; mostly due to the way that Golang statically includes libraries, meaning the binaries get very big quite quickly. In Golang there is a strong cultural bias towards random developers doing everything from raw; polar opposite to PHP and JS.

I have to create everything to do POST multipart uploads. There are plenty of webpages saying how to send a multipart form; but none on receiving them.

stdlib Components

  • mime
  • mime/multipart
  • labstack/echo ~ Ok, yes I did install that one extra
  • net/http
  • io
// this function doesn't iterate if you have uploaded multiple things in 1 POST

func GetMulitPartPostFile(ctx echo.Context, formname string) (string, err) {
	boundary 	:= ctx.request().Header.Get("Content-Type")
	mtype, list, err:=mine.ParseMediaType(boundary)
	boundary	=list["boundary"]

	partreader  := multipart.NewReader( ctx.Request().Body, boundary)
	buf			:= make([]byte, A_SIZE_CONST)
	part, err	:= partreader.NextPart()
	if err ==io.EOF {
		// log error
		return "", ctx.NoContent(400)
	}
	if part == nil {
		// log error
		return "", ctx.NoContent(400)
	}
	if part.FormName() != formname {
		return "", io.EOF
	}
	
	sz, err		= part.Read(buf)
	if err == io.EOF && sz==0 {
		// log error
		return "", ctx.NoContent(400)
	}
	if sz==0 || sz==A_SIZE_COUNT {
		// log error
		return "", ctx.NoContent(400)	
	}
	useful		:= string(buf)
	useful		= strings.Trim(useful, "\t \n\r\x00")
	return useful, nil
}

Some similar articles in research