Skip to content

Instantly share code, notes, and snippets.

View pulkitkumar's full-sized avatar
🎯
Focusing

Pulkit Kumar pulkitkumar

🎯
Focusing
View GitHub Profile
@pulkitkumar
pulkitkumar / Question.md
Last active October 25, 2019 09:26
Interview test question.

The following interface fetches articles for an id.

interface NetworkApi {
    /*
     * Fetches details of an article of parameter [id] from the network.
     */  
    fun article(id: String) : Single<Article>
}
@pulkitkumar
pulkitkumar / handle_network_error.go
Created August 13, 2018 09:51
Handling Network error
func networkRequest() {
body, err := makeRequest("url", "GET")
if err != nil {
switch err := err.(type) {
case *NetworkError:
if err.IsAccessDenied() {
// handle access denied here.
} else if err.IsAuthError() {
// handle auth error here.
@pulkitkumar
pulkitkumar / network_error.go
Last active August 13, 2018 11:32
Network Error
// Network error, when the status code from a network request is > 400
type NetworkError struct {
statusCode int
Body string
}
func (n *NetworkError) IsAuthError() bool {
return n.statusCode == 401
}
@pulkitkumar
pulkitkumar / error_handling.go
Last active August 13, 2018 07:06
Simple error handling
n, err := io.ReadAtLeast(r, buf, min)
if err != nil {
switch err {
case io.ErrShortBuffer:
// handle short buffer error
break
case io.ErrUnexpectedEOF:
// handle short buffer error
break
default:
@pulkitkumar
pulkitkumar / user_pointer_return_main.go
Created August 10, 2018 12:38
Main function using a function that returns a pointer.
type User struct {
Name string
Email string
}
func main() {
var u *User
u, err := fetchStruct()
if err == nil {
modifyEmail(u)
@pulkitkumar
pulkitkumar / value_sharing.go
Last active August 10, 2018 12:50
GoLang Value Sharing
package main
import (
"net/http"
"github.com/pkg/errors"
)
type User struct {
Name string
func fetchStruct() (*User, error) {
var u User
resp, err := http.Get("url")
if err != nil {
return &u, errors.Wrap(err, "network read error")
}
u = User{
Name: "some data from resp",
Email: "some data from resp",
}
package user
import (
"net/http"
"github.com/pkg/errors"
)
type User struct {
Name string
@pulkitkumar
pulkitkumar / golang_semantics.go
Created August 6, 2018 18:13
GoLang Semantics
package user
import (
"io"
)
type User struct {
Name string
Email string
}
@pulkitkumar
pulkitkumar / oop_semantics.go
Last active August 7, 2018 02:21
OOP Semantics
package user
import (
"io"
)
type User struct {
Name string
Email string
}