C
C
cybervito212014-11-23 19:49:01
MySQL
cybervito21, 2014-11-23 19:49:01

How to organize code on GO?

How to write code has examples of creating your own package.
The application works with MySQL and consists of main.go and a pair of package.
The question is how to make it so that there is one variable (or a package with a getter method) containing a connection to MySQL and it can be accessed from packages.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2014-11-27
@cybervito21

In its simplest form, you can do this:
you create a package, for example db, in which you work with the connection. Here, create a reference variable to *sql.DB. You
create a regular public function that returns this variable.
More or less like this:

var connection *sql.DB

func GetConnection() *sql.DB {
        return connection
}

When you start the program, you create a connection. Next, you import db from any package and get the connection: conn := db.GetConnection()
UPD: in the GetConnection function, you can check the current connection and, if necessary, restore it:
func GetConnection() *sql.DB {
        connection.Ping()

        return connection
}

See /database/sql/

G
game802, 2017-02-27
@l55uiz

First, we need to read the documentation and make sure everything works correctly...
Try writing a form with this solution:

<form id="myForm">
    <input id='experience' name="experience" value=""/>
    <input type="submit" value="Go"/>
</form>

$(function() {
   $.validator.addMethod("regex", function(value, element, regexpr) {          
     return regexpr.test(value);
   }, "Пожалуйста, введите верное значение");    
    
   $("#myForm").validate({
       rules: {
           experience: {
               required: true,
               regex: /^(\s*)?(\+)?([- _():=+]?\d[- _():=+]?){8,14}(\s*)?$/
           }
       }
   });
});

And we must not forget to put /at the beginning and at the end of the regular expression. Those. should turn out like this:
regex: '/^(\s*)?(\+)?([- _():=+]?\d[- _():=+]?){8,14}(\s*)?$/',

S
Snewer, 2017-02-27
@Snewer

pattern: '/^(\s*)?(\+)?([- _():=+]?\d[- _():=+]?){8,14}(\s*)?$/'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question