avatarPolly Clover

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5070

Abstract

do that:</p><div id="2e8b"><pre>eventHandler<span class="hljs-selector-class">.Handle</span>(event1, <span class="hljs-built_in">ExampleMiddlewareFoo</span>(<span class="hljs-built_in">ExampleMiddlewareBar</span>(handle)))</pre></div><h1 id="a8a5">Managing Events with Handlers and Middleware</h1><p id="f869">Most articles I read explain how to use this design pattern; this one deals with implementing the internal logic. So let's start coding.</p><p id="35c4">The full code for this article can be found <a href="https://github.com/xNok/slack-go-demo-socketmode/blob/main/examples/middleware/main.go">here</a> to help you follow along.</p><h2 id="0f88">Designing events</h2><p id="b622">First, we are going to enumerate the list of events that our system can handle. The way we create enumeration in Go is a bit different than in other programming languages. In Go, we are going to use a set of constants sharing the same <code>type</code>. Here I define a type <code>EventType</code>that represents a string with the event's name.</p><div id="b181"><pre>// <span class="hljs-keyword">type</span> <span class="hljs-type">used </span>to enumerate events <span class="hljs-keyword">type</span> <span class="hljs-type">EventType </span>string</pre></div><div id="4817"><pre>const ( event1 EventType <span class="hljs-operator">=</span> <span class="hljs-string">"event1"</span> event2 EventType <span class="hljs-operator">=</span> <span class="hljs-string">"event2"</span> )</pre></div><p id="e041">Next, we define the event itself. In our example, the<code>Event</code> as a type which can be selected among the list of <code>EventType</code> created above.</p><div id="2bc9"><pre><span class="hljs-keyword">type</span> <span class="hljs-type">Event</span> struct { <span class="hljs-type">Type</span> <span class="hljs-type">EventType</span> <span class="hljs-type">Data</span> interface{} }</pre></div><h2 id="df6c">Create an event sender (for test purpose)</h2><p id="60aa">To test our system, we will need to create a small function to send events every 2s. Each<code>Event</code> is transmitted via a <a href="https://tour.golang.org/concurrency/2">channel</a> and, the <code>eventSender</code> below sends a random <code>Event</code> of type <code>event1</code> or <code>event2</code> to a channel.</p><blockquote id="53f3"><p><i>Channels are a type which you can send and receive values, they are great for communication among goroutines. In other words, there are perfect for sending and receive event through your application.</i></p></blockquote><div id="ffd1"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">eventSender</span><span class="hljs-params">(c <span class="hljs-keyword">chan</span> EventType)</span></span> {</pre></div><div id="7f57"><pre> for { // Send <span class="hljs-selector-tag">a</span> random event <span class="hljs-selector-tag">to</span> the channel rand<span class="hljs-selector-class">.Seed</span>(<span class="hljs-selector-tag">time</span><span class="hljs-selector-class">.Now</span>()<span class="hljs-selector-class">.Unix</span>()) events := []EventType{ event1, event2, } n := rand.<span class="hljs-built_in">Int</span>() % <span class="hljs-built_in">len</span>(events)</pre></div><div id="6101"><pre> c <- events[n] <span class="hljs-comment">// send event to channel</span></pre></div><div id="5285"><pre> // <span class="hljs-keyword">wait</span> a <span class="hljs-built_in">bit</span> <span class="hljs-built_in">time</span>.Sleep(<span class="hljs-number">2</span> * <span class="hljs-built_in">time</span>.Second) } }</pre></div><h2 id="000c">Handler and dispatcher</h2><p id="e562">We first need a struct to hold the list of events we want to listen to and which function to call whenever that event is transmitted. This struct also contains the channel used for communicating events.</p><div id="280d"><pre><span class="hljs-comment">// Create a struct to hold config</span> <span class="hljs-comment">// And simplify dependency injections</span> <span class="hljs-keyword">type</span> EventHandler <span class="hljs-keyword">struct</span> { <span class="hljs-comment">// Event channel</span> Events <span class="hljs-keyword">chan</span> Event <span class="hljs-comment">// hold the registedred event functionss</span> EventMap <span class="hljs-keyword">map</span>[EventType][]<span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(Event)</span></span> }</pre></div><p id="aa6c">Next, we need to provide an initializing constructor for our <code>EventHandler</code>.</p><div id="a4ed"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">NewEventHandler</span><span class="hljs-params">()</span></span> *EventHandler { eventMap := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[EventType][]<span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(Event)</span></span>) event

Options

s := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> Event)</pre></div><div id="78bb"><pre> return <span class="hljs-variable">&</span>EventHandler<span class="hljs-punctuation">{</span> <span class="hljs-symbol"> Events:</span> events, <span class="hljs-symbol"> EventMap:</span> eventMap, <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span></pre></div><p id="98fe">Then, we can create our <code>Handle</code> function that associates the event with a callback function.</p><div id="308c"><pre><span class="hljs-comment">// register the handler function to handle an event type</span> func (h *EventHandler) <span class="hljs-built_in">Handle</span>(e EventType, f <span class="hljs-built_in">func</span>(Event)) { h<span class="hljs-selector-class">.EventMap</span><span class="hljs-selector-attr">[e]</span> = <span class="hljs-built_in">append</span>(h<span class="hljs-selector-class">.EventMap</span><span class="hljs-selector-attr">[e]</span>, f) }</pre></div><p id="e42a">Finally, we create the <code>EventDispatcher</code> function, the core of this mechanism. The <code>EventDispatcher</code>process any event sent to a channel, check its type, and if any function has been registering for that type, we call all registered functions.</p><div id="6597"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(h *EventHandler)</span></span> EventDispatcher() { <span class="hljs-keyword">for</span> evt := <span class="hljs-keyword">range</span> h.Events { log.Printf(<span class="hljs-string">"event recieved: %v"</span>, evt) <span class="hljs-keyword">if</span> handlers, ok := h.EventMap[evt.Type]; ok { <span class="hljs-comment">// If we registered an event</span> <span class="hljs-keyword">for</span> _, f := <span class="hljs-keyword">range</span> handlers { <span class="hljs-comment">// exacute function as goroutine</span> <span class="hljs-keyword">go</span> f(evt) } } } }</pre></div><h1 id="13e4">Using our system</h1><p id="4d4e">Everything is ready; we can start using our event handling system.</p><ol><li>Instantiate our event Handler</li><li>Register which event to listen to and what function to callback</li><li>Start the event sender</li><li>Start the event dispatcher</li></ol><div id="62ab"><pre><span class="hljs-keyword">func</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> {</pre></div><div id="5b09"><pre> <span class="hljs-variable">eventHandler</span> := <span class="hljs-function"><span class="hljs-title">NewEventHandler</span>()</span></pre></div><div id="d244"><pre> eventHandler.Handle(event1, <span class="hljs-keyword">func</span><span class="hljs-params">()</span> { <span class="hljs-built_in">log</span>.Printf(<span class="hljs-string">"event Handled: %v"</span>, event1) })</pre></div><div id="5c56"><pre> <span class="hljs-variable">go</span> <span class="hljs-function"><span class="hljs-title">eventSender</span>(<span class="hljs-variable">eventHandler.Events</span>)</span></pre></div><div id="30f2"><pre> eventHandler<span class="hljs-selector-class">.EventDispatcher</span>()</pre></div><div id="9bf2"><pre>}</pre></div><p id="e590">The result should be along those lines:</p><figure id="5ee7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_D-GCRSu3K8CgTBXewJjJQ.gif"><figcaption></figcaption></figure><p id="19d6">Since we only handle an event of type <code>event1</code>, only <code>event1</code> shows as <code>Hansled</code>. All is good!</p><p id="d48a">The full code for this article can be found <a href="https://github.com/xNok/slack-go-demo-socketmode/blob/main/examples/middleware/main.go">here</a>.</p><h1 id="6f5e">Interesting Articles tackling the same topic</h1><ul><li><a href="https://drstearns.github.io/tutorials/gomiddleware/">Middleware Patterns in Go</a></li><li><a href="https://sathishvj.medium.com/web-handlers-and-middleware-in-golang-2706c2ecfb75">Web Handlers and Middleware in GoLang</a></li><li><a href="https://readmedium.com/lightweight-event-management-implemented-by-go-a654d59ac65">Lightweight event management implemented by Go</a></li><li><a href="https://readmedium.com/the-7-most-important-software-design-patterns-d60e546afb0e">The 7 Most Important Software Design Patterns</a></li></ul><p id="13e7"><i>Do you want <b>unlimited access</b> to all my content and many other writers’ content on Medium? Consider using my affiliate link to <a href="https://couedeloalexandre.medium.com/membership">become a Medium member today</a></i></p><h2 id="853a">Further reads</h2> <figure id="81d2"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://couedeloalexandre.medium.com/embed/list/3f9d03f2cb8f" allowfullscreen="" frameborder="0" height="184" width="undefined"> </div> </div> </figure></iframe></div></div></figure></article></body>

What Being a Teacher Taught Me About Education

Sad truths of the education system in the U.S…

Photo by Morning Brew on Unsplash

Being a teacher was always my dream. Sure, I was the cliche school-girl who always said “when I grow up, I’m going to be a teacher.” And, that’s what I did.

Being an educator is not for the faint of heart.

Now, I know there are many people out there who actually do believe that “those who can’t do, teach.” Those people are sorely mistaken. Teachers can do a lot. A whole hell of a lot more than many.

Teachers are counselors. Teachers are friends. Teachers are stand-in parents. Teachers are mediators. Teachers are tutors. Teachers are mentors. Teachers are volunteers. Teachers are nurses. Teachers are entertainers. Teachers are zookeepers.

I could truly go all day, but you may sort of have the point now. The thing is, I know I can say this all day long but unless you do it, you can’t fathom the many hats and the total madness that is teaching unless you experience it. And, that’s okay.

Sad Truths of the Education System

I cannot speak for everywhere and every educator, of course. I know my own experiences as a public school teacher in South Carolina. I also know the experiences of friends who are educators and the experiences of other teachers who have written about them. From who I’ve encountered and from what other educators have said, I know the education system in the United States is completely screwed up.

The Students Don’t Matter

I cannot begin to express how often I fought, begged, and pleaded to do what was best for my students and was literally not allowed to.

Seb is a perfect example of a student that did not matter and he was one of too many. Seb is a nickname. I call him Seb because, in an entire school year, all he learned to write was ‘Seb’ for his name. This was a significant accomplishment and I’m still so proud of him to this day.

Seb had many issues from minute one of stepping into my classroom. Dad walked him in on the first day of first grade. He had already gone to 4k and Kindergarten so he wasn’t new to this. Seb didn’t speak English (I taught in a predominantly Hispanic school), but his dad did and I learned that Seb had lived in the United States all of his life and his teachers from the two years prior worked very hard with him. I could tell immediately that Seb was terrified and may be different than what was ‘normal’ for a 6-year-old.

In the first few days of school, he never said a word. He never made eye-contact. He used the bathroom in his pants. He didn’t participate in any activities.

Throughout the school year, I was gentle and patient. I worked with Seb every day. I documented anything and did everything I could. I asked for assistance from interventionists, Special Ed teachers, and my principal at least weekly.

At testing time (Fall and Winter), Seb scored as low as one could possibly score. Remember, I taught Hispanic students so many of them scored below grade level because the testing was in ENGLISH and they spoke SPANISH. Makes sense, right? I’ll save this rant another day. Instead of this documentation being more ammo for receiving support for Seb, I was told I needed to do more.

I spent the entire school year finding any second of the day to help him one-on-one. I stayed up late at night creating personal activities for him. I contacted the district office to let them know this child was not making progress and really needed serious interventions. I met with his dad a lot. His dad was devastated and desperately wanted help for his child.

When the end of the year came, and his final standardized test AND my other documentation (because I don’t think a test is the end-all, be-all — imagine that) showed Seb had made close to zero progress and was very behind even after having been in school for 3 years, I was told that because he was Hispanic and the problems were language-related that nothing could be done.

I didn’t know being scared of a school bathroom has to do with language, but okay? This child really needed more support than I was equipped for and he never received it. Two years later, I still think about him regularly.

Seb is only one example. I have a hundred more examples of students needing more and being completed neglected by the education system.

The School System is Racist

Remember how the administration said that Seb was Hispanic, therefore his challenges were due to a language barrier? Well, no, I had more documentation that a teacher may have ever had on a student and I had an insane amount of proof that it was more than a language barrier. Language doesn’t prevent someone from being able to properly hold a pencil, feeling comfortable enough to use the bathroom in a toilet, acknowledging being called by the name they’ve had their whole life.

On top of this personal experience of racism in schools, there’s also plenty of research that shows that Black students and other students of color are more likely to be suspended or expelled. I can’t say for certain I have experienced this because I never taught white students.

However, there’s AW. AW was a boy I had my second year of teaching. AW turned my whole world upside down. And, my classroom. AW became a student at my school well into the school year. He was in first grade and had already been to multiple schools. He had lived in upwards of 10 foster homes. He now lived with his very old and sickly grandfather. What a sweet man he was!

AW had been abused by his parents and several foster parents. He had been severely neglected. My administration knew all of this about AW before he came into my classroom because they told me about it (the day before). Even still, they tossed him into my room bright and early and told me, “good luck.” They really did say that. We all needed more than luck.

He spent the day kicking me, screaming at everyone, spitting on other students, flipping furniture, you name it. I clicked the call button in my classroom a lot that day and was mostly ignored. Over the next few months, these behaviors continued. Some days were better than others. Some days, he ran away. He got out of the building a couple of times.

He eventually received some help outside of the classroom. He had an interventionist who he worked with for about half an hour a day and he left after lunchtime when his grandfather suggested shortened days like his old school.

AW also got suspended a lot. Why would a kid who really needs intense therapy and a whole lot of love be kicked out of his only safe space? I mean, his grandfather's house was probably the only safe home AW had ever entered, but his grandfather could not take care of him due to his age and illnesses. I know this for a fact because I visited him at home when he was out of school for an extended period of time to make sure they were both okay.

My friends who teach in predominantly white schools have told me how a black child will be expelled for doing the same action a white child has done with little to no consequence. Studies show that black students are far more likely to be expelled than white students. Being expelled from school, and I know from experience, rarely changes behaviors or has any other positive outcome. So, why is it even a common consequence?

Let’s not forget about educational redlining. While redlining is now illegal, its effects are still present. Schools in low-income neighborhoods (neighborhoods where black students and students of color live due to redlining in the past) are vastly underfunded. Again, I know this to be a fact due to experience. All schools not having an equal amount of money to educate students regardless of color is, in fact, also racism.

More Robots Please

My time as a classroom teacher reiterated for me that school is actually robot training. This may sound dramatic, sure. Teachers as individuals can encourage creativity and individuality to an extent. Kind of. Doing this risks consequences.

I would work effortlessly on lesson plans and oftentimes be questioned by them. Why are the students doing this fun craft? What purpose does this science experiment have? How is this preparing them for the ‘test?’ Is that on the ‘test?’ ‘What kind of learning do you intend to happen on a nature walk?’

I used to notice how one of my schools posted class nature walks and art projects on their Facebook page, but I still felt hesitant to do these things. It’s like they knew parents wanted to see this stuff. Or maybe they knew activities like this fostered important life-long skills. But, they were worried that doing this meant that the students' test scores would make the school look bad so deep-down they discouraged out-of-the-box lessons.

It’s kind of embarrassing what strategies (daily math drills, worksheet packets that equaled hundreds of chopped down trees, and lollipops) the teachers and administration would come up with at staff meetings to make sure our school’s standardized test scores were the best. They never were the best, though.

The majority of school years were spent meeting about, talking about, making plans for, and having nightmares about standardized tests. When test time came around (3 times per year and for 1–2 weeks each time), pep rallies were held ranting cheers about doing well on the test, prizes were delivered to students who met almost always unattainable goals (set by people who have probably never stepped foot in a classroom), and talks were had to those who didn’t meet goals.

Here’s the thing I always wondered and still do. Why on Earth will these 6-year olds ever need to know “if Tommy eats 327 apples and Suzie eats 215 apples, how many more apples did Tommy eat than Suzie?” If anything, isn’t their mind going to be focused on how one can eat that many apples and why not blueberries? Or what about the fact that a non-English speaking student is expected to read a story that’s not even on a first-grade level for students that do speak English, and then answer questions about it? And, that my friends, is how we determine if children are ‘smart enough.’

If we are teaching students to be robots, why not have robots teach our students and save everyone a lot of time and a little bit of money?

That’s Not It

The list of sad truths of the education system doesn’t end there. There’s more including the barely-livable salary, lack of funding, mental health issues (students and educators), lack of support from parents and administration, lack of diversity, outdated teaching methods, school safety issues, severe lack of resources for those with disabilities, and high dropout rate. I’m sure I’m missing a few, too.

What Do We Do?

I don’t know. That’s why I quit. I felt helpless. I still do. I don’t want to never teach again when I miss being in the classroom almost daily. But, I can’t see being a willing participant in the deterioration of my mental health and the utter disservice to students who truly deserve so much more.

I know good, passionate teachers are what those children need. I feel sorrow in not being able to be that person.

I think parents need to start or continue to be advocates for their children. I think teachers who still have it in them must keep fighting. I think all citizens who care about our future need to put the views and plans politicians have for education at the top of their list. I think those in power (administration) of schools need to stop being bossed around by people who care more about politics than education and start doing what’s right for the children.

How do parents feel? I can’t fathom. This issue is a huge contribution to why I don’t want to have children of my own.

Children are our greatest treasure. They are our future.

When will we begin to treat them as such?

Education
Education Reform
Schools
Children
Teaching
Recommended from ReadMedium