A note on how to improve the autodetection script for Go

Back to General discussions forum

oezguery     2023-08-15 19:52:54
User avatar

Hi friends!

Autodetection script is not detecting Go at all!. To fix this, I would like to replace the coditional statement:

if (s.indexOf('var ') >= 0) {
    return s.indexOf('using ') >= 0 ? 'C#' : 'JavaScript';
}

with

if (s.indexOf('var ') >= 0) {
    return s.indexOf('using ') >= 0 || s.indexOf('Main ') >= 0 || s.indexOf('Console.') >= 0 ? 'C#' 
    s.indexOf('func ') >= 0 && s.indexOf('package ') >= 0 ? 'Go' : 'JavaScript';
}
Rodion (admin)     2023-08-16 14:17:40
User avatar

Hi again! Curious that you thought of Go - actually there are a number of languages which are not detected... Though go is one of the most "promising" candidates, I myself switched to it from Java couple years ago :)))

Your snippet seems to be slightly malformed - after the second line there should be colon probably, or not there, I'm a bit slow to understand. Perhaps let's rewrite it with separate ifs (inside the outer one)?

Overall I support the idea! Perhaps it would be good even to set up some test-ground for variants of language detector. Wanted to try it with JsFiddle, but currently it won't open for me.

oezguery     2023-08-16 20:21:02
User avatar

Hi Rodion,

I am learning Go over the vacation. You are right about the snippet, it needs a colon at the end of the second line. As you suggested I would put it inside a nested if statement.

Go makes me a lot of fun. I am learning by solving puzzles and doing small exercises. Doing a few puzzles here already helped me.

Rodion (admin)     2023-08-17 06:17:51
User avatar

Aha, thanks for confirmation!

I replaced the code as you suggested, and it looks like less or more working (also it attempts to improve recognition of C# and JS as I understand). However I dare say var is not very often found in Go code, mostly for global variables. Thus we may later come up with more improvements.

Perhaps the detection code needs general reworking in something more streamlined and flexible :) also the logic when detection happens may need improvement - for now it tries to decide when user entered only few lines, and this may lead to misidentifications...

Doing a few puzzles here already helped me.

Actually, struggling through it these two years, I found the language is mainly valued for "goroutines" and "channels" out of the box (most other languages include similar things only in form of heavy library methods) - and thus these "single-threaded" exercises may not be adequate to unveil these "main powers". Language itself is somewhat clumsy. Feel free to ping me when you get a bit tired of practicing syntax, so I can share some questions or puzzles on Go which I met in interviews.

oezguery     2023-08-17 07:56:47
User avatar

Good morning!

Go is indeed clumsy when it comes to solving puzzles. I enjoy learning in general and it is a great training to reach a goal under constraints.

When I come to more advanced stuff I will sure ping you. Right now I am learning the basics. Exercism is a good website for getting comments on the solutions from "mentors". I am making use of it to learn new concepts and improve my knowledge.

About the detection code, I cannot say much because I have experience with only a few languages.

oezguery     2023-08-17 11:32:46
User avatar

Right now, all Go submissions without the var keyword are being detected as C#. To prevent this, I recommend inserting at an appropriate place into the detector script a condition such as

if (s.indexOf("package main") >= 0) {
    return 'Go';
}
Please login and solve 5 problems to be able to post at forum