Skip to content

Instantly share code, notes, and snippets.

View yakuter's full-sized avatar
💭
Working #golang @binalyze

Erhan Yakut yakuter

💭
Working #golang @binalyze
View GitHub Profile
@yakuter
yakuter / multipart-pipe.go
Last active September 27, 2022 08:42
Multipart and Pipe file stream
// This example shows how to send files via pipe using multipart package
package main
import (
"errors"
"fmt"
"io"
"mime/multipart"
"os"
@yakuter
yakuter / empty-struct-2.go
Last active November 3, 2022 19:40
Empty Struct 2
// Versiyon 2
package main
import "fmt"
type Foo struct{}
func main() {
a := &Foo{}
b := &Foo{}
@yakuter
yakuter / empty-struct-1.go
Last active November 3, 2022 19:40
Empty Struct
// Versiyon 1
package main
import "fmt"
type Foo struct{}
func main() {
a := &Foo{}
b := &Foo{}
@yakuter
yakuter / error-handling.swift
Last active March 13, 2023 10:03
Swift error handling
// Method 1
struct User {
}
enum SecurityError: Error {
case emptyEmail
case emptyPassword
}
@yakuter
yakuter / Logger.swift
Created March 13, 2023 10:08
A useful logger struct in swift
// Usage
let logger = Logger("YakuterPackage.ViewController")
logger.info("Result: %{public}@", result)
logger.error("This is an error log")
// Logger.swift file content
@yakuter
yakuter / convert.go
Created July 28, 2020 00:07
String Byte Conversion Without Memory Allocation
// b2s converts byte slice to a string without memory allocation.
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func b2s(b []byte) string {
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&b))
}
@yakuter
yakuter / cancel-io-copy.go
Created April 25, 2021 19:25
IO Copy cancellation
// Source: https://ixday.github.io/post/golang-cancel-copy/
import (
"io"
"context"
)
// here is some syntaxic sugar inspired by the Tomas Senart's video,
// it allows me to inline the Reader interface
type readerFunc func(p []byte) (n int, err error)
@yakuter
yakuter / copy.go
Created February 14, 2024 11:05
Copy and Movement 1
src := []int{1, 2, 3, 4, 5}
dst := make([]int, 5)
// copy from slice to slice
copied := copy(dst, src)
// copied: 5
// dst: [1 2 3 4 5]
// copy from string to slice
var b = make([]byte, 5)
@yakuter
yakuter / append.go
Created February 14, 2024 15:13
Copy and Movement 2
package main
import "fmt"
func main() {
var s []int
// len=0 cap=0 []
s = append(s, 0) // append works on nil slices.
// len=1 cap=1 [0]
@yakuter
yakuter / ioPipe.go
Created February 15, 2024 07:54
Copy and Movement 3
package main
import (
"fmt"
"io"
"os"
)
func main() {
reader, writer := io.Pipe()