Skip to content

Instantly share code, notes, and snippets.

@gophersumit
gophersumit / go-stdlib-interface-selected.md
Created November 19, 2020 17:21 — forked from asukakenji/go-stdlib-interface-selected.md
Go (Golang) Standard Library Interfaces (Selected)

Go (Golang) Standard Library Interfaces (Selected)

This is not an exhaustive list of all interfaces in Go's standard library. I only list those I think are important. Interfaces defined in frequently used packages (like io, fmt) are included. Interfaces that have significant importance are also included.

All of the following information is based on go version go1.8.3 darwin/amd64.

@gophersumit
gophersumit / response.go
Created February 2, 2020 04:05
Response struct in Go
// source : https://golang.org/src/net/http/response.go?s=731:4298#L25
type Response struct {
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
Header Header
Body io.ReadCloser
ContentLength int64
@gophersumit
gophersumit / request.go
Created February 2, 2020 03:19
Request Struct in Go
// source : https://golang.org/src/net/http/request.go?s=3252:11812#L97
type Request struct {
Method string
URL *url.URL
Proto string // "HTTP/1.0"
ProtoMajor int // 1
ProtoMinor int // 0
Header Header
Body io.ReadCloser
GetBody func() (io.ReadCloser, error)
@gophersumit
gophersumit / server.go
Last active January 31, 2020 03:54
Server struct from Go std library
// from https://golang.org/src/net/http/server.go?s=77156:81268#L2480
// A Server defines parameters for running an HTTP server.
// The zero value for Server is a valid configuration.
type Server struct {
Addr string // TCP address to listen on, ":http" if empty
Handler Handler // handler to invoke, http.DefaultServeMux if nil
TLSConfig *tls.Config
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
@gophersumit
gophersumit / defer.go
Last active June 18, 2019 03:58
using defer keyword
package main
import (
"log"
"os"
)
func main() {
file, err := os.Open("file.go")
//handle errors first if any
@gophersumit
gophersumit / openfile.go
Created June 18, 2019 03:00
File open operation in Go
package main
import (
"log"
"os"
)
func main() {
file, err := os.Open("file.go") // For read access.
if err != nil {
@gophersumit
gophersumit / goswitch.go
Created June 18, 2019 01:47
switch statement in go
switch expression
{
case expression_value1:
Statement
case expression_value2:
Statement
case expression_value3:
Statement
fallthrough // cam use fallthrough to explicitly define fallthrough behavior
@gophersumit
gophersumit / csharptswitch.cs
Created June 18, 2019 01:40
Switch statement in C#
switch (expression)
{
case expression_value1:
Statement
break; // required to stop next case from executing
case expression_value2:
Statement
break; // required to stop next case from executing
case expression_value3:
Statement
@gophersumit
gophersumit / ifusage.go
Last active June 15, 2019 08:09
Braces for conditionals and loops
package main
import "fmt"
func main() {
isTrue := true
if isTrue // if statement must have braces even if statement is single line
fmt.Println("This is invalid")
@gophersumit
gophersumit / increment.go
Created June 15, 2019 07:54
Increment operator in go
package main
import "fmt"
func main() {
x := 0
x++ //valid operation
fmt.Println(x++) // invalid operation x++ is not an expression
++x // invalid operation , prefix increment does not exist in go