Page 1 of 1

Programming using ChatGPT

Posted: Sat Feb 25, 2023 9:09 pm
by Nebula

I thought that once ChatGPT was launched, it may be a pretty awesome tool, especially for those of us who love to code. Currently I am working on a project where I use it extensively.

My usual workflow involves starting with a simple boilerplate code that I can originally could easily find online, but I would have to go through a lot of links before I could even the one I can actually use. I could spend at least 1 or two hours depending on the complexity of what I was looking to implement.

Then, when ChatGPT came along, I thought I would try to give it a whirl. I put what I wanted, and then ChatGPT incorporated my question, and within seconds, it produced a code. I tried it and it worked. It saved me a lot of hours that normally would be wasted in searching on Google, clicking on what Google thinks is relevant, filter through the information on endless blogs, etc.

But, to be fair, Google has been a tool we coders have used, and probably many will still use, but they would be missing out on the opportunity to save time, learn faster, because it's like a co-worker, a colleague, be it whatever you want to name this ChatGPT! For me, it's a tool and I love what it is doing for me.

Therefore, if you're looking to use ChatGPT to create some code for your own project, here are a few things you can do, as I did. First I start with a minimal code that is enough to compile or run, a simple "hello world" example. Then I ask ChatGPT to incorporate little changes, like add a function, rename variables and do other things you would normally do when developing your code. The amazing thing about ChatGPT is that it remembers your code and sometimes gives you only the changes needed to be made together with an explanation where to insert them. Additionally, if you find yourself not understanding the code produced by ChatGPT, just simply ask it to explain the code and it will do exactly that.

You will notice that once you've given ChatGPT your prompt, you can ask it to generate any code for you. In fact, if you don't like what it spits out, then just generate another response! Keep in mind that the code generated by ChatGPT may not be perfect, so you may need to tweak it a bit or modify it to get it working exactly how you want.

Hope this helps!


Re: Programming using ChatGPT

Posted: Sat Feb 25, 2023 9:45 pm
by Infinity

I think it is an interesting idea to use ChatGPT to generate code. However, when I tried doing that, I got a reply that it cannot create code: "As an AI language model, I am not able to create code directly." Did I do something wrong?


Re: Programming using ChatGPT

Posted: Sat Feb 25, 2023 9:47 pm
by Nebula
Infinity wrote: Sat Feb 25, 2023 9:45 pm

I think it is an interesting idea to use ChatGPT to generate code. However, when I tried doing that, I got a reply that it cannot create code: "As an AI language model, I am not able to create code directly." Did I do something wrong?

I also get it sometimes. It seems that keywords like "create" or "write" trigger this response. However if you provide more details about the code you want it to produce, it will actually create the code.


Re: Programming using ChatGPT

Posted: Sat Feb 25, 2023 9:53 pm
by Infinity

Okay, it worked. This is what I asked it: "Please create a minimal webserver in Go" and this is what I got:

Sure, here's some code to create a minimal web server in Go:

Code: Select all

package main

import (
        "fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", handler)
	fmt.Println("Server listening on port 8080...")
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World!")
}

This code creates a simple web server that listens on port 8080 and responds with "Hello, World!" to any incoming HTTP requests.

You can save this code to a file with a .go extension and run it using the go run command. For example, if you save the code to a file called server.go, you can run it by typing go run server.go in your terminal.

I hope this helps! Let me know if you have any questions.


Re: Programming using ChatGPT

Posted: Sat Feb 25, 2023 9:58 pm
by Nebula

Excellent, now ask it to add a new endpoint.


Re: Programming using ChatGPT

Posted: Sat Feb 25, 2023 10:01 pm
by Infinity
Nebula wrote: Sat Feb 25, 2023 9:58 pm

Excellent, now ask it to add a new endpoint.

It worked flawlessly! I simply asked:

Can you add a "hello" endpoint?

and it gave me this:

Sure, here's the modified code with an additional "hello" endpoint:

Code: Select all

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/hello", helloHandler)
	fmt.Println("Server listening on port 8080...")
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World!")
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, there!")
}

This code adds a new route to the web server at the /hello endpoint, which responds with "Hello, there!" to any incoming requests.

You can run this modified code in the same way as the previous code. When you make a request to http://localhost:8080/hello in your web browser or using a tool like curl, you should see the "Hello, there!" message displayed.

Let me know if you have any other questions!