Skip to content

Commit

Permalink
golang-snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
jolav committed Oct 7, 2024
1 parent 5ce0b71 commit 09d1fca
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 40 deletions.
73 changes: 73 additions & 0 deletions docs/golang-snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ func IsJSON(str string) bool {
}
```

### IsPointer

```go
func IsPointer(d interface{}) bool {
return reflect.TypeOf(d).Kind() == reflect.Ptr
}
```

---

## FILES
Expand Down Expand Up @@ -840,6 +848,23 @@ func GetIP(r *http.Request) string {
}
```

### GetRequestOrigin

```go
func GetRequestOrigin(r *http.Request) string {
switch {
case r.Header.Get("Host") != "":
return r.Header.Get("Host")
case r.Header.Get("Origin") != "":
return r.Header.Get("Origin")
case r.Header.Get("Referer") != "":
return r.Header.Get("Referer")
default:
return "?????"
}
}
```

### IsValidURL

```go
Expand Down Expand Up @@ -925,6 +950,54 @@ func RemoveProtocolAndWWWL(url string) string {
}
```

### Nginx return 444

```go
func close(w http.ResponseWriter, r *http.Request) {
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Server does not support hijacking", http.StatusInternalServerError)
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
conn.Close()
return
}
```

### Slow Response

```go
func slowSend(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
err := "Server does not support flusher"
http.Error(w, err, http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Initiating slow response...")

for i := 0; i < 100; i++ {
fmt.Fprintf(w, "Fragmento %d\n", i+1)
flusher.Flush()
time.Sleep(1 * time.Second)
}

fmt.Fprintln(w, "Task accomplished")
}

func holdConn(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Begin.............")
time.Sleep(60 * time.Second)
fmt.Fprintf(w, "Thats all")
}
```
---

## NUMBERS
Expand Down
2 changes: 1 addition & 1 deletion www/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.134713+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.313159+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.176982+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.335780+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/debian/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.195883+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.343454+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/expressjs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.219100+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.353656+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.230371+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.361212+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/golang-bases-de-datos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.236388+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.363150+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/golang-para-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.240116+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.364793+00:00
-->
</body>

Expand Down
104 changes: 103 additions & 1 deletion www/golang-snippets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ <h3 id="isjson">IsJSON</h3>
return json.Unmarshal([]byte(str), &amp;js) == nil
}
</code></pre>
<h3 id="ispointer">IsPointer</h3>
<pre><code class="language-go">func IsPointer(d interface{}) bool {
return reflect.TypeOf(d).Kind() == reflect.Ptr
}
</code></pre>
<hr />
<h2 id="files">FILES</h2>
<h3 id="readfile">ReadFile</h3>
Expand Down Expand Up @@ -806,6 +811,20 @@ <h3 id="getip">GetIP</h3>
return strings.TrimSpace(ip)
}
</code></pre>
<h3 id="getrequestorigin">GetRequestOrigin</h3>
<pre><code class="language-go">func GetRequestOrigin(r *http.Request) string {
switch {
case r.Header.Get(&quot;Host&quot;) != &quot;&quot;:
return r.Header.Get(&quot;Host&quot;)
case r.Header.Get(&quot;Origin&quot;) != &quot;&quot;:
return r.Header.Get(&quot;Origin&quot;)
case r.Header.Get(&quot;Referer&quot;) != &quot;&quot;:
return r.Header.Get(&quot;Referer&quot;)
default:
return &quot;?????&quot;
}
}
</code></pre>
<h3 id="isvalidurl">IsValidURL</h3>
<pre><code class="language-go">// IsValidURL ...
func IsValidURL(rawurl string) bool {
Expand Down Expand Up @@ -876,6 +895,49 @@ <h3 id="removeprotocolandwwwfromurl">RemoveProtocolAndWWWFromURL</h3>
return url
}
</code></pre>
<h3 id="nginx-return-444">Nginx return 444</h3>
<pre><code class="language-go">func close(w http.ResponseWriter, r *http.Request) {
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, &quot;Server does not support hijacking&quot;, http.StatusInternalServerError)
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
conn.Close()
return
}
</code></pre>
<h3 id="slow-response">Slow Response</h3>
<pre><code class="language-go">func slowSend(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
err := &quot;Server does not support flusher&quot;
http.Error(w, err, http.StatusInternalServerError)
return
}

w.Header().Set(&quot;Content-Type&quot;, &quot;text/plain&quot;)
fmt.Fprintln(w, &quot;Initiating slow response...&quot;)

for i := 0; i &lt; 100; i++ {
fmt.Fprintf(w, &quot;Fragmento %d\n&quot;, i+1)
flusher.Flush()
time.Sleep(1 * time.Second)
}

fmt.Fprintln(w, &quot;Task accomplished&quot;)
}

func holdConn(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, &quot;Begin.............&quot;)
time.Sleep(60 * time.Second)
fmt.Fprintf(w, &quot;Thats all&quot;)
}
</code></pre>
<hr />
<h2 id="numbers">NUMBERS</h2>
<h3 id="getrandomint">GetRandomInt</h3>
Expand Down Expand Up @@ -1663,6 +1725,11 @@ <h3>Contenidos</h3>
<li><a href="#isjson">IsJSON</a></li>
<ul>
</ul>
<li><a href="#ispointer">IsPointer</a></li>
<ul>
</ul>
</ul>
Expand Down Expand Up @@ -1738,6 +1805,11 @@ <h3>Contenidos</h3>
<li><a href="#getip">GetIP</a></li>
<ul>
</ul>
<li><a href="#getrequestorigin">GetRequestOrigin</a></li>
<ul>
</ul>
<li><a href="#isvalidurl">IsValidURL</a></li>
Expand All @@ -1763,6 +1835,16 @@ <h3>Contenidos</h3>
<li><a href="#removeprotocolandwwwfromurl">RemoveProtocolAndWWWFromURL</a></li>
<ul>
</ul>
<li><a href="#nginx-return-444">Nginx return 444</a></li>
<ul>
</ul>
<li><a href="#slow-response">Slow Response</a></li>
<ul>
</ul>
</ul>
Expand Down Expand Up @@ -2106,6 +2188,11 @@ <h3>Contenidos</h3>
<li><a href="#isjson">IsJSON</a></li>
<ul>

</ul>

<li><a href="#ispointer">IsPointer</a></li>
<ul>

</ul>

</ul>
Expand Down Expand Up @@ -2181,6 +2268,11 @@ <h3>Contenidos</h3>
<li><a href="#getip">GetIP</a></li>
<ul>

</ul>

<li><a href="#getrequestorigin">GetRequestOrigin</a></li>
<ul>

</ul>

<li><a href="#isvalidurl">IsValidURL</a></li>
Expand All @@ -2206,6 +2298,16 @@ <h3>Contenidos</h3>
<li><a href="#removeprotocolandwwwfromurl">RemoveProtocolAndWWWFromURL</a></li>
<ul>

</ul>

<li><a href="#nginx-return-444">Nginx return 444</a></li>
<ul>

</ul>

<li><a href="#slow-response">Slow Response</a></li>
<ul>

</ul>

</ul>
Expand Down Expand Up @@ -2372,7 +2474,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.251456+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.369573+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/golang/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3125,7 +3125,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.265657+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.374695+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.292052+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.388722+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ <h3>Quick search</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.175601+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.335019+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/javascript-apis/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.300788+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.394065+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/javascript-para-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.310242+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.401772+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/javascript-snippets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.332687+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.411797+00:00
-->
</body>

Expand Down
2 changes: 1 addition & 1 deletion www/javascript/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ <h3>Contenidos</h3>

<!--
MkDocs version : 1.4.2
Docs Build Date UTC : 2024-10-04 13:57:34.337295+00:00
Docs Build Date UTC : 2024-10-07 20:02:36.414807+00:00
-->
</body>

Expand Down
Loading

0 comments on commit 09d1fca

Please sign in to comment.