Code Monkey home page Code Monkey logo

go-chat's Introduction

go-chat

Go, chat.

Overview

This was a rather in-depth introduction to the Go language. The code is more verbose and performs more actions than it probably has to, as this is my first foray into the language, but it does demonstrate many concepts of the language and gave me a good excuse to deep-dive some of the core features of Go.

It's worth noting that I prefer to be "thrown into the fire" as it were when it comes to learning new languages. The code in this repo was gleaned from comparison to knowledge in Node.js and searching for equivalent concepts. I did not participate in any bootcamps or courses to assemble the code in this repository. Overall, I really enjoyed getting to know the language.

Inspiration for server commands and UX were taken straight from using IRC back in the day.

Go Concepts Covered

Though this list may not be complete, these are some concepts that stood out to me:

  • Reflection
  • Wrapping struct
  • Stand-alone struct
  • Struct methods
  • Package Scope
  • Third-Party library imports
  • Pointers and references
  • Slices
  • Standard Library methods

App Requirements

  • Spin up a server
  • Connect via telnet
  • Send messages to server from client
  • Relay messages to all client
  • Messages (from other users) should have timestamp and username prefix
  • Messages (from server and other users) logged to file
  • Configuration from file (using .env pattern)

Usage

In a terminal window, run:

$ go run ./pkg

In a separate (or multiple) terminal window, run:

$ telnet 0.0.0.0 55555

You should see messages from the telnet client, and the server which look something like:

Trying 0.0.0.0...
Connected to 0.0.0.0.
Escape character is '^]'.
๐Ÿš€ Go Chat v0.1.0

> What shall we call you?

Type your desired username, and press return/enter. You'll then be shown the available commands for the server. Go bananas from there.

Key Struct Notes

app

  • contains buuld metadata

Some habits are hard to let go of. I've long been a fan of package.json in Node.js and not including some kind of build/package metadata feels wrong.

ChatChannel

  • manages messaging the channel

ChatClient

  • implements the methods that correspond to available commands
  • handles sending data to the client
  • listens to the channel
  • dispatches server functions

ChatConnection

  • handles I/O between server and client

ChatServer

  • wraps net.Listener
  • handles new client connections
  • manages channels

Room for Improvement / Research

  • Keep auxiliary Go files in separate directory from main.go
  • Use reflection (if possible) to detect how many arguments a ChatClient method has, and throw an error if the user passes more (or less) arguments than the method requires
  • The multiline string for usage isn't printing correctly. Must be a Go thing
  • Handle an abrupt client disconnect, such as ^] signal in telnet
  • Prevent two users from connecting at the same username
  • Learn more about the repercussions and performance issues surrounding pointers

go-chat's People

Contributors

shellscape avatar zeshana avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.