<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dbarun</title>
    <description>The latest articles on DEV Community by dbarun (@wahengba).</description>
    <link>https://dev.to/wahengba</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1034684%2F7cdcd189-ea46-44bd-849c-1b4dd4d1c94a.png</url>
      <title>DEV Community: dbarun</title>
      <link>https://dev.to/wahengba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wahengba"/>
    <language>en</language>
    <item>
      <title>Brute Force way of Looping Combination</title>
      <dc:creator>dbarun</dc:creator>
      <pubDate>Tue, 21 May 2024 08:18:26 +0000</pubDate>
      <link>https://dev.to/wahengba/brute-force-way-of-looping-combination-1jmo</link>
      <guid>https://dev.to/wahengba/brute-force-way-of-looping-combination-1jmo</guid>
      <description>&lt;p&gt;How Do We LOOP C(r, n) Lets loop C(3,6)&lt;br&gt;
In Combination we don't care about order so the formula of combination [n!/r!(n-r)!] gives C(3, 6) value of  = 6!/3!x3! = 20&lt;/p&gt;

&lt;p&gt;We already know we need to loop 3 times as we selected 3 items but Sometimes I confused what will the array bound and It turns out to be very simple to know the array bound &lt;/p&gt;

&lt;p&gt;Let's take an array of [1, 2, 3, 4, 5, 6]&lt;br&gt;
Here r = 3 and n = 6 = len(arr) &lt;/p&gt;

&lt;p&gt;for the first loop the array bound will be like this&lt;br&gt;
I will use go/golang and the classic for loop to write &lt;br&gt;
&lt;code&gt;for i := 0; i &amp;lt;= len(arr) - 3; i++{}&lt;/code&gt;&lt;br&gt;
or &lt;br&gt;
&lt;code&gt;for i := 0; i &amp;lt; len(arr) - 2; i++{}&lt;/code&gt;&lt;br&gt;
the boundary should be &amp;lt;= len(arr) - r or &amp;lt; len(arr) - (r-1)&lt;/p&gt;

&lt;p&gt;and the second will be &lt;br&gt;
&lt;code&gt;for j := i+1; j &amp;lt;= len(arr) - 2; i++{}&lt;/code&gt;&lt;br&gt;
or &lt;br&gt;
&lt;code&gt;for i := i+1; j &amp;lt; len(arr) - 1; i++{}&lt;/code&gt;&lt;br&gt;
the boundary should be &amp;lt;= len(arr) - r-1 or &amp;lt; len(arr) - (r-2)&lt;/p&gt;

&lt;p&gt;I guess now you can figure out the sequence or the formula for this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp43inbbsa9vjea8hh2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp43inbbsa9vjea8hh2r.png" alt="Image description" width="734" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[the second variation]&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycnc1ck6015bmesvemc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycnc1ck6015bmesvemc5.png" alt="Image description" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>loops</category>
      <category>beginners</category>
      <category>combinations</category>
    </item>
    <item>
      <title>SRR React With Go and Templ</title>
      <dc:creator>dbarun</dc:creator>
      <pubDate>Fri, 01 Mar 2024 23:06:28 +0000</pubDate>
      <link>https://dev.to/wahengba/using-reacthtmx-is-better-with-go-templ-1afj</link>
      <guid>https://dev.to/wahengba/using-reacthtmx-is-better-with-go-templ-1afj</guid>
      <description>&lt;p&gt;for more ref &lt;a href="https://github.com/BarunW/form_backend/tree/main/integration-react" rel="noopener noreferrer"&gt;https://github.com/BarunW/form_backend/tree/main/integration-react&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I am here to share the process and It's already on the templ documentation &lt;a href="https://templ.guide/media/" rel="noopener noreferrer"&gt;https://templ.guide/media/&lt;/a&gt;, but i have some issue on that I change here and there.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install temp &lt;code&gt;go install github.com/a-h/templ/cmd/templ@latest&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;add &lt;code&gt;go get github.com/a-h/templ&lt;/code&gt; to your project&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;now make a dir/folder inside your go project e.g react-integration &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cd react-integration and make two dir/folder static(where your templ and templ geneated go file should be) and another one react()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;inside static create a page.tmpl&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;make sure you have data to pass if you need the data to render,&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package static
   import(
      "github.com/username/myrepo/types"
  )
   */
script renderIndex(bp types.BillingPlan) {
    // Use the renderIndex function from the Reactbundle
    bundle.renderIndex()
}

templ Index(bp types.BillingPlan) {
    &amp;lt;div id="index"&amp;gt;&amp;lt;/div&amp;gt;  
    // in templ you this to call a templ component or script  
    @renderIndex(bp)
}


templ Page(bp types.BillingPlan) {
`&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;React integration&amp;lt;/title&amp;gt;
            &amp;lt;link rel="stylesheet" href="static/index.css" 
            &amp;lt;/link&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;div id="react-header"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div id="react-content"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
        &amp;lt;/div&amp;gt;  
           // this index.js will be generate by es build 
        &amp;lt;script src="static/index.js"&amp;lt;/script&amp;gt;
            @Index(c, fs) 
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;`
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this run command&lt;code&gt;** templ generate**&lt;/code&gt; so that it will generate the required go code( make sure inside static dir/folder)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;inside react do npm init -y make this files tsconfig.json
and install necessary package(ts, esbuild, react, react-dom)
&lt;code&gt;or copy pasta this  
{
"name": "react-integration",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"author": "",
"license": "ISC",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react-dom": "^18.2.18",
"esbuild": "0.20.1",
"npm-run-all": "^4.1.5",
"typescript": "^5.3.3"
}
}&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;we are using tsx for bundle so we use react for jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
      "jsx": "react", 
      "lib": ["ESNext", "DOM"],
      "esModuleInterop": true,
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;create your required files for me i will create components.tsx , index.tsx and app.tsx
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;components.tsx
`import React from "react";

export function Component(props:any){
    return(
    &amp;lt;div&amp;gt;
       &amp;lt;h3 onClick={()=&amp;gt; console.log(data)&amp;gt;Click Me&amp;lt;/h3&amp;gt;
    &amp;lt;/div&amp;gt;
    )
}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;app.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import  React, { useEffect, useRef, useState } from 'react';
import { Component } from "./components"

const App = (props : any) =&amp;gt;{
// do whatever you want
 return(
   &amp;lt;Component billingPlan={props}/&amp;gt;
 )
}
export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;index.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactDOM from 'react-dom/client';
import React from 'react';
import App from './app';

export function renderIndex(content:any){
    const root = ReactDOM.createRoot(document.getElementById('index') as HTMLElement);
    root.render(
       &amp;lt;App content={content} /&amp;gt;
    );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;generate the index.js bundle using this command &lt;code&gt;./node_modules/.bin/esbuild --bundle index.tsx --outdir=../static --minify --global-name=bundle&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now we have to serve I like to use echo framework
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import(
    "github.com/labstack/echo/v4"
    "github.com/username/types"
    "net/http"

)

func main(){

  e := echo.New() 

  // middleware for serving static file
  e.Static("/static", "./integration-react/static")

  // routes 
  e.Any("/shit",echo.HandlerFunc(func(c echo.Context) error {
   // call the template component in the static
   templComp := static.Page(types.BillingPlan{})
   Render(c, http.StatusOK, templComp) 
  }))

  s := http.Server{
       Addr:        ":8080",
       Handler:     e,
   }
  if err := s.ListenAndServe(); err != http.ErrServerClosed {
    log.Fatal(err)
  }

}

// this is the echo integration with templ code
func Render(ctx echo.Context, statusCode int, tc templ.Component) error {

    ctx.Response().Writer.WriteHeader(statusCode)
    ctx.Response().Header().Set(echo.HeaderContentType, 
                                     echo.MIMETextHTML)
    return tc.Render(ctx.Request().Context(), 
                             ctx.Response().Writer)

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>go</category>
      <category>backend</category>
      <category>ssr</category>
    </item>
    <item>
      <title>Go http handlers</title>
      <dc:creator>dbarun</dc:creator>
      <pubDate>Tue, 29 Aug 2023 01:01:11 +0000</pubDate>
      <link>https://dev.to/wahengba/setting-up-a-go-http-server-1k5b</link>
      <guid>https://dev.to/wahengba/setting-up-a-go-http-server-1k5b</guid>
      <description>&lt;p&gt;A short walk on how to setup a go server using HTTP package provided by itself. Before jumping to framework I recommend you to explore and understand the packages provided by the particular programming language.&lt;/p&gt;

&lt;p&gt;Before jumping directly to the code we need to know how go &lt;br&gt;
structure the HTTP package so we need to jump to another code &lt;br&gt;
i.e server.go &lt;br&gt;
[]&lt;a href="https://dev.tourl"&gt;(https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/net/http/server.go)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I extract the code from the above link in case if you are too lazy to have another tab &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;serveMux&lt;/strong&gt; which type is struct
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ServeMux struct {
    mu    sync.RWMutex
    m     map[string]muxEntry
    es    []muxEntry // slice of entries sorted from longest to shortest.
    hosts bool       // whether any patterns contain hostnames
}

type muxEntry struct {
    h       Handler
    pattern string
}

// NewServeMux allocates and returns a new ServeMux.
func NewServeMux() *ServeMux { return new(ServeMux) }

// DefaultServeMux is the default ServeMux used by Serve.
var DefaultServeMux = &amp;amp;defaultServeMux

var defaultServeMux ServeMux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Handler&lt;/strong&gt;
&lt;code&gt;type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request) signature
}&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.&lt;strong&gt;Handle&lt;/strong&gt; i.e func (mux *ServeMux) Handle(path string, handler Handler)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (mux *ServeMux) Handle(pattern string, handler Handler) {
    mux.mu.Lock()
    defer mux.mu.Unlock()

    if pattern == "" {
        panic("http: invalid pattern")
    }
    if handler == nil {
        panic("http: nil handler")
    }
    if _, exist := mux.m[pattern]; exist {
        panic("http: multiple registrations for " + pattern)
    }

    if mux.m == nil {
        mux.m = make(map[string]muxEntry)
    }
    e := muxEntry{h: handler, pattern: pattern}
    mux.m[pattern] = e
    if pattern[len(pattern)-1] == '/' {
        mux.es = appendSorted(mux.es, e)
    }

    if pattern[0] != '/' {
        mux.hosts = true
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.&lt;strong&gt;HandleFunc&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    if handler == nil {
        panic("http: nil handler")
    }
    mux.Handle(pattern, HandlerFunc(handler))
}

// Handle registers the handler for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }

// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    DefaultServeMux.HandleFunc(pattern, handler)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;lets write the code to set up the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "os"
    "os/signal"
    "time"
)

type foo struct {
  l *log.Logger
}

// we can create a constructor function
func newFoo(l *log.Logger) *foo{
   return &amp;amp;(l)
}

/* we need to implement a ServeHTTP function in order to use  http.Handle because it takes handler as argument.Handler is an interface which has ServeHTTP signature ref 2*/

func (f *foo) ServeHTTP(rw http.ResponseWriter, r*http.Request){
   // handle all sort of things
   rw.Write([]byte("Hello"))
   return
}


func main() {

    l := log.New(os.Stdout, "go", log.LstdFlags)
    nf := newFoo(l)

    /* we can create a new serveMux if not in listenAndServe, 
    default sever is use  */
     sm := http.NewServeMux()

// routing using Handle
     sm.Handle("/",nf) 

//routing using HandleFunc 
     sm.HandleFunc("/hf",func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("handle by Handle Func"))
        return
    })

    // this server configuartion
    s := &amp;amp;http.Server{
        Addr: ":3000",
        Handler: sm,
        IdleTimeout : 120*time.Second,
        ReadTimeout: 1 * time.Second,
        WriteTimeout: 1*time.Second,
    }

    // we use go routine in order to independently execute 
    // and separate from main go routine
    go func() {

        err := s.ListenAndServe()
        if err != nil {
            l.Fatal(err)
        }
    }()

    /* in order to shutdown the server properly means if we want to shutdown the server it make the existing request respond properly*/
    sigChan := make(chan os.Signal) 
    signal.Notify(sigChan, os.Interrupt)
    signal.Notify(sigChan,os.Kill)

    sig := &amp;lt;- sigChan //this line blocks the waits for signal
    fmt.Println("gracefully shutting down server", sig)
    tc, f :=context.WithTimeout(context.Background(), 30*time.Second)
    f()
    s.Shutdown(tc)

}

---

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all :) &lt;/p&gt;

</description>
      <category>go</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
